sql - MySQL LEFT JOIN Query with WHERE clause -
hope can having hard time understanding how query properly
i have member table , member_card table. member_card has column member, card associated member. both tables have lastmodifieddate column. member can have none, 1 or several cards.
i need return members lastmodifieddate >= sincedate (given date) or card (if any) lastmodifieddate >= sincedate.
imagine sincedate 2013-01-01 00:00:00. want output this:
[{ "id": "001o000000fsas7iaf", "lastmodifieddate": 2013-01-01 00:00:00, "member_card": null }, { "id": "001o000000frpixiaz", "lastmodifieddate": 2012-12-12 00:00:00, "member_card": [{ "id": "a00o0000002w8fgiay", "member": "001o000000fhdsoian", "lastmodifieddate": 2013-01-01 00:00:00 }, { "id": "a00o0000002uymtiam", "member": "001o000000fhdsoian", "lastmodifieddate": 2012-12-12 00:00:00 }] }, { "id": "001o000000fsag7iaf", "lastmodifieddate": 2013-01-01 00:00:00, "member_card": [{ "id": "a00o0000002w8ffiay", "member": "001o000000fhdsoian", "lastmodifieddate": 2012-12-12 00:00:00 }] }]
the 1st member matching lastmodifieddate without cards. 2nd member non-matching lastmodifieddate, has 2 cards associated, , 1 of them has matching lastmodifieddate. 3rd 1 member matching lastmodifieddate card.
thanks got following query:
select member.*,card.* member inner join ( select distinct member.id mid member inner join member_card on member_card.member = member.id , ( member.lastmodifieddate >= sincedate or member_card.lastmodifieddate >= sincedate ) ) on a.mid=member.id inner join member_card card on card.member=a.mid
which works fine missing case member doesn't have card associated.
i tried change inner joins left joins it's ignoring date comparison :-(
can me one?
you want move date check of inner query.
try like:
select member.*,card.* member left join member_card card on card.member=member.id member.id in ( select distinct m.id member m left join member_card mc on (mc.member = m.id) ( m.lastmodifieddate >= sincedate or mc.lastmodifieddate >= sincedate ) )
Comments
Post a Comment