如何使用SQL执行可选JOIN

时间:2015-05-11 03:08:28

标签: mysql sql tsql join

我想从biometrics返回所有行,并在JOIN存在时获取额外数据。但是,我的当前查询仅在用户标识匹配时返回行。

如何解决此问题,以便返回所有生物识别行,以及来自JOIN的额外数据(如果存在)?

SELECT b.id as id, g.date
FROM `biometrics` as b
INNER JOIN `users goals` as g ON biometricid = b.id and userid = $user->id

1 个答案:

答案 0 :(得分:4)

使用左外连接:

SELECT b.id as id, g.date
FROM `biometrics` as b
LEFT OUTER JOIN `users goals` as g ON biometricid = b.id and userid = $user->id

可以找到不同联接类型的好例子here