0

我有 2 个表:Member 和 Member_Card。Member_Card 有一列链接到 Member Id 列,因此 Member 与 Member_Card 有一对多的关系

我输出一个 JSON,例如:

{
    "Name" : "Member Name",
    "Cards": [
            { "Card_No" : "1234"},
            { "Card_No" : "4321"}
         ]
}

Member 和 Member_Card 都有一个 LastModifiedDate 列。

我的查询是这样的:

SELECT Name FROM Member 
LEFT JOIN Member_Card 
    ON Member_Card.Member = Member.id 
WHERE Member.LastModifiedDate >= sinceDate 
    OR Member_Card.LastModifiedDate >= sinceDate

这个查询的问题是它只会返回自 sinceDate 以来修改的卡/成员(即,如果一个成员有 2 张卡并且一张在 sinceDate 之后被修改,它只会返回 1 张卡)。我想退回会员及其所有卡片,无论是卡片还是会员本身在 sinceDate 之后被修改。

我怎么能这样查询?还是我需要运行 2 个查询?

4

1 回答 1

0
SELECT member.*,c.* from member inner join (
SELECT distinct member.id as  mid FROM Member 
INNER JOIN Member_Card 
    ON Member_Card.Member = Member.id 
and ( Member.LastModifiedDate >= sinceDate  
    OR Member_Card.LastModifiedDate >= sinceDate ) ) a on a.mid=member.id
inner join member_card c on c.member=a.mid
于 2013-08-05T03:55:37.817 回答