从连接表中选择数据

时间:2011-05-12 15:37:23

标签: php mysql sql join outer-join

我有两张桌子,我正在尝试使用左外连接获取信息。

我有以下查询:

SELECT * 
  FROM sportjefit_user 
  LEFT OUTER JOIN vriend ON sportjefit_user.id = vriend.vriend2

并且结果如下: enter image description here

我只想要'vriend1'或'vriend2'不是48的记录

有人可以帮帮我吗?

感谢。

3 个答案:

答案 0 :(得分:2)

SELECT * FROM sportjefit_user 
LEFT OUTER JOIN vriend ON sportjefit_user.id = vriend.vriend2
WHERE vriend1 <> 48
OR vriend2 <> 48

或者,如果您想确保两个字段都不是48

SELECT * FROM sportjefit_user 
LEFT OUTER JOIN vriend ON sportjefit_user.id = vriend.vriend2
WHERE vriend1 <> 48
AND vriend2 <> 48

答案 1 :(得分:1)

添加一个where clause,它几​​乎可以映射到您所说的内容:

SELECT * 
FROM sportjefit_user LEFT OUTER JOIN vriend ON sportjefit_user.id=vriend.vriend2
  where !(vriend1=48 or vriend2=48) 
  or (vfriend1 is null and vfriend2 is null);

答案 2 :(得分:1)

试试这个:

SELECT * FROM sportjefit_user 
LEFT OUTER JOIN vriend 
ON sportjefit_user.id = vriend.vriend2
WHERE ((vriend.vriend1<>48) AND (vriend.vriend2<>48))