mysql - Need Help Thinking Through Semi-Complex Query on Two Tables -


i have 2 tables, , want create select pulling single record 1 table based on multiple records table.

perhaps clearer if give sort of example.

table: dates. fields: dosid, personid, name, datein

1 | 10 | john smith | 2013-09-05 2 | 10 | john smith | 2013-01-25 

table: cards. fields: cardid, personid, cardcolor, carddate

1 | 10 | red    | 2013-09-05 2 | 10 | orange | 2013-09-05 3 | 10 | black  | 2013-09-05 4 | 10 | green  | 2013-01-25 5 | 10 | orange | 2013-01-25 

so want select record dates table if person did not receive "red" card. closest have come like:

select name, datein dates, cards  dates.personid = cards.personid , cardcolor != 'red' , datein = carddate; 

but query 2013-09-05 date-of-service still pulled out because of "orange" , "black" cards given on same day.

i have tried searching not sure how describe issue , google-fu has failed me. or suggestions appreciated.

the easiest understand version filter using not exists:

select name, datein   dates  not exists(          select *            cards           cards.personid = dates.personid             , cards.carddate = dates.datein             , cards.cardcolor = 'red'        ) 

see on sqlfiddle.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -