MySQL Join AND EXISTS 组合
MySQL Join AND EXISTS in combination
个案
我收到以下查询:
SELECT * FROM `parking_parking`
JOIN `parking_address` ON `parking_parking`.`parking_address` = `parking_address`.`address_id`
WHERE `parking_id` = 3
此查询选择有关停车场的信息(有关停车场本身的地址和数据)
总的来说..它有效!
问题
虽然有一个小问题..
每当地址已从数据库中删除并且停车场本身仍然存在时。整个查询 returns 0。只是因为它查找和 A AND B 链接。但是,如果找不到一个,第二个也不会返回。
现在有解决办法了..
存在
但是我不知道怎么用
我试过了:
EXISTS JOIN
JOIN EXISTS
JOIN `parking_address` ON EXISTS
但是没有用。
我希望(并猜测)我忽略了一件小事。
Note
!! I do not use this in real life! !!
SELECT * FROM
I did this one when I was still at the veeeery basics and I found out the hard way that even the simplest pages took ages to load.
解决方案:GolezTrol
SELECT * FROM `parking_parking`
LEFT JOIN `parking_address` ON `parking_parking`.`parking_address` = `parking_address`.`address_id`
WHERE `parking_id` = 3
将join
(inner join
的缩写)改为left join
(=left outer join
)。这将 return 所有 parkings,如果没有匹配的 address[=20],地址字段将 return null
=]:
SELECT * FROM `parking_parking`
LEFT JOIN `parking_address`
ON `parking_parking`.`parking_address` = `parking_address`.`address_id`
WHERE `parking_id` = 3
个案 我收到以下查询:
SELECT * FROM `parking_parking`
JOIN `parking_address` ON `parking_parking`.`parking_address` = `parking_address`.`address_id`
WHERE `parking_id` = 3
此查询选择有关停车场的信息(有关停车场本身的地址和数据)
总的来说..它有效!
问题
虽然有一个小问题.. 每当地址已从数据库中删除并且停车场本身仍然存在时。整个查询 returns 0。只是因为它查找和 A AND B 链接。但是,如果找不到一个,第二个也不会返回。
现在有解决办法了.. 存在
但是我不知道怎么用
我试过了:
EXISTS JOIN
JOIN EXISTS
JOIN `parking_address` ON EXISTS
但是没有用。
我希望(并猜测)我忽略了一件小事。
Note
!! I do not use this in real life! !!
SELECT * FROM
I did this one when I was still at the veeeery basics and I found out the hard way that even the simplest pages took ages to load.
解决方案:GolezTrol
SELECT * FROM `parking_parking`
LEFT JOIN `parking_address` ON `parking_parking`.`parking_address` = `parking_address`.`address_id`
WHERE `parking_id` = 3
将join
(inner join
的缩写)改为left join
(=left outer join
)。这将 return 所有 parkings,如果没有匹配的 address[=20],地址字段将 return null
=]:
SELECT * FROM `parking_parking`
LEFT JOIN `parking_address`
ON `parking_parking`.`parking_address` = `parking_address`.`address_id`
WHERE `parking_id` = 3