MySQL,SQL Select语句,OR的位置......这有什么问题?

时间:2010-03-09 05:09:26

标签: sql mysql

我正在寻找下面的查询帮助。从来没有为veggie返回任何东西...我的WHERE语句写得有效吗?

SELECT *
FROM newsfeed INNER JOIN newsfeedaction ON newsfeed.newsfeedactionid = newsfeedaction.newsFeedActionID
  INNER JOIN person ON newsfeed.personID = person.personID
  LEFT OUTER JOIN food ON newsfeed.foodID = food.foodID
  LEFT OUTER JOIN veggie ON newsfeed.veggieID = veggie.veggieID
WHERE  
  (
  newsfeed.veggieID IS NOT NULL
  AND veggie.deleted = 'N'
  )
 OR
  (
  newsfeed.foodID IS NOT NULL
  AND food.deleted = 'N')

3 个答案:

答案 0 :(得分:1)

where子句不完整。第二组条件即将完成。

答案 1 :(得分:1)

除非VEGGIE.veggieid可以为null(同样适用于FOOD.foodid),否则请使用:

SELECT *
  FROM NEWSFEED nf
  JOIN NEWSFEEDACTION nfa ON nfa.newfeedactionid  = nf.newsfeedactionid
  JOIN PERSON p ON p.personid = nf.personid
  JOIN FOOD f ON f.foodid = nf.foodid
             AND f.deleted = 'N'
  JOIN VEGGIE v ON v.veggieid = nf.veggieid
               AND v.deleted = 'N'

该查询是正确的,但要求NEWSFEED记录必须在VEGGIEFOOD表中都有支持记录。

答案 2 :(得分:-1)

两组数据之间的联盟起了作用,我在网上看到,两个选项之间的UNION比一组有两个数组的速度更快