Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel查询生成器选择相同的表相同的列具有不同的条件_Php_Mysql_Sql_Laravel_Query Builder - Fatal编程技术网

Php Laravel查询生成器选择相同的表相同的列具有不同的条件

Php Laravel查询生成器选择相同的表相同的列具有不同的条件,php,mysql,sql,laravel,query-builder,Php,Mysql,Sql,Laravel,Query Builder,我有两个表格,例如“用户”和“课程”。 我想选择通过两门课程的用户 user id username 1 John 2 Jane courses id name user_id 1 course-1 1 2 course-2 1 3 course-1 2 我使用: (new User)->join('courses', 'courses.user_id', '=', 'user.id') ->whereIn('courses.name', [

我有两个表格,例如“用户”和“课程”。 我想选择通过两门课程的用户

user               
id username
1 John
2 Jane

courses
id name user_id
1 course-1 1
2 course-2 1
3 course-1 2    
我使用:

(new User)->join('courses', 'courses.user_id', '=', 'user.id')
->whereIn('courses.name', ['course-1','course-2'])
但是这个查询返回2个用户,而不是1个(只有john)。我想要的是返回和连接,而不是连接

我想要这样:

 SELECT * from user 
    join courses as c1 on c1.user_id = user.id
    join courses as c2 on c2.user_id = user.id
    where c1.name = 'course-1' and c2.name = 'course-2'
在Laravel中的情况如何?

请使用以下选项:

User::join('courses as c1', 'c1.user_id', '=', 'user.id')
    ->join('courses as c2', 'c2.user_id', '=', 'user.id')
    ->where('c1.name', 'course-1')
    ->where('c2.name', 'course-2')

顺便说一句:你应该在这样的情况下使用。

你是说
c2.user\u id=user.id
?哦,对不起,是的。非常感谢。