Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.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
返回MySQL连接上的所有项目,而不仅仅是匹配的项目_Mysql_Join - Fatal编程技术网

返回MySQL连接上的所有项目,而不仅仅是匹配的项目

返回MySQL连接上的所有项目,而不仅仅是匹配的项目,mysql,join,Mysql,Join,结帐 | item_id | user_id | 项目 我需要一个查询,该查询将返回所有项目,并带有一列,如果它存在于某个特定用户id的签出中,则返回1。否则它将返回0 select * from checkouts right join items on items.item_id = checkouts.item_id where checkouts.user_id = 10 问题是它只返回它所连接的项,而不是所有的项 有什么想法吗?那 SELECT i.item_id, c.user

结帐

| item_id | user_id |
项目

我需要一个查询,该查询将返回所有项目,并带有一列,如果它存在于某个特定用户id的签出中,则返回1。否则它将返回0

select * 
from checkouts
right join items on items.item_id = checkouts.item_id
where checkouts.user_id = 10
问题是它只返回它所连接的项,而不是所有的项

有什么想法吗?

SELECT i.item_id, c.user_id, IF(c.user_id IS NULL, 0, 1) AS extra_column
FROM items i
LEFT JOIN checkouts c ON (c.item_id = i.item_id AND c.user_id = 10)
那么

SELECT i.item_id, c.user_id, IF(c.user_id IS NULL, 0, 1) AS extra_column
FROM items i
LEFT JOIN checkouts c ON (c.item_id = i.item_id AND c.user_id = 10)

尝试左外连接:

select * from items left outer join checkouts using (item_id)
 where checkouts.user_id is null or checkouts.user_id = 10
或者,您也可以使用:

select * from items i 
  left outer join checkouts c
    on (i.item_id=c.item_id and c.user_id=10)

尝试左外连接:

select * from items left outer join checkouts using (item_id)
 where checkouts.user_id is null or checkouts.user_id = 10
或者,您也可以使用:

select * from items i 
  left outer join checkouts c
    on (i.item_id=c.item_id and c.user_id=10)

尝试一个
右侧外部连接
,例如

select * 
from checkouts c
right outer join items i on i.item_id = c.item_id
where c.user_id = 10

尝试一个
右侧外部连接
,例如

select * 
from checkouts c
right outer join items i on i.item_id = c.item_id
where c.user_id = 10

不行。您需要
c.user\u id为null
作为条件之一。。。否则,
left join
是没有意义的。您也可以在
on
子句中添加该条件,我想它应该可以工作:
on(c.item_id=I.tiem_id和c.user_id=10)
@PabloSantaCruz,是的,谢谢您捕捉到这一点。根据您的建议更新。无效。您需要
c.user\u id为null
作为条件之一。。。否则,
left join
是没有意义的。您也可以在
on
子句中添加该条件,我想它应该可以工作:
on(c.item_id=I.tiem_id和c.user_id=10)
@PabloSantaCruz,是的,谢谢您捕捉到这一点。根据您的建议进行更新。