代码之家  ›  专栏  ›  技术社区  ›  Bruno Francisco

获取满足这两种语言筛选器的所有用户

  •  0
  • Bruno Francisco  · 技术社区  · 6 年前

    我想抓住所有说 Portuguese English .

    SELECT * 
           FROM   `users_language` 
           WHERE  `users`.`id` = 
              `users_language`.`user_id` 
               AND ( `language_id` = 'en'
               OR `language_id` = 'pt' )
    

    我在用 Laravel , , German 等等。。。这就是为什么我有以下代码:

    $candidates->whereHas('user.languages', function ($q) use ($request){
                        foreach($request->language as $index => $language_code){
                            $q->where('language_id', $language_code);
    //                        $q->where('speaking', '>=', $request->speaking[$index]);
    //                        $q->where('writing', '>=', $request->writting[$index]);
    //                        $q->where('comprehension', '>=', $request->comprehension[$index]);
                        }
                    });
    

    第一个查询是为那些不理解的人提供的简化版本 Laravel/PHP

    SELECT `user_id`, 
           `cover_letter`, 
           `selected` 
    FROM   `jobs_application` 
    WHERE  `jobs_application`.`job_id` = ? 
           AND `jobs_application`.`job_id` IS NOT NULL 
           AND EXISTS (SELECT * 
                       FROM   `users` 
                       WHERE  `jobs_application`.`user_id` = `users`.`id` 
                              AND EXISTS (SELECT * 
                                          FROM   `users_language` 
                                          WHERE  `users`.`id` = 
                                                 `users_language`.`user_id` 
                                                 AND `language_id` = ? 
                                                 AND `language_id` = ?))
    

    每当我选择一个说 我不是唯一一个会说两种语言的用户。我想检索说过滤器提供的语言的用户。

    whereIn() 但这不会让我以后通过 speaking writing comprehension

    1 回复  |  直到 6 年前
        1
  •  1
  •   nakov    6 年前

    尝试这样的事情,我在查询中评论,所以请让我知道,如果它会给你一个如何使用它的想法。

    这将选择不同的用户id,这些id与使用过滤器提供的所有语言的所有用户相匹配。可以在select子句中检索更多的列,但还需要在GROUPBY子句中使用相同的列。

    select u.user_id from users as u
    inner join users_language as ul
    on ul.user_id = u.user_id
    where ul.language_id in (1, 6, 7, 11) # IDs of the languages, you can continue the WHERE clause here by adding AND appending your different cases.
    group by u.user_id
    having count(ul.language_id) = 4; # number 4 comes from the total count of languages in the IN clause. I have 4 ids there, hence 4 here.