tags:

views:

39

answers:

3

I'm looking for help with my query below. which is never returning anything for veggie... Is the way I have my WHERE statement written valid?

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')
+1  A: 

The where clause is incomplete. The second set of conditions are to be completed.

Kangkan
ok that was a bad paste on my end. I'm updating the above since that's not the issue
AnApprentice
Rewrite the query as provided by OMG Ponies and also check if there is any data that meets the condition. As data is not provided, it can not be checked by others.
Kangkan
+1  A: 

Unless the VEGGIE.veggieid can be null (likewise for FOOD.foodid), use:

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'

The query is otherwise correct, but requires that NEWSFEED records must have supporting records in both the VEGGIE and FOOD tables.

OMG Ponies
A: 

A Union between the two sets of data did the trick and I read online that UNION between two selects is faster than a WHere with 2 groups

AnApprentice