开发者

Query Construction Combining UNION and LEAST

开发者 https://www.devze.com 2023-03-06 21:39 出处:网络
I have an email subscription table and a user table.I need to combine the two to get all the emails, since it\'s possible to create an account without subscribing and vice versa.Easy enough so far:

I have an email subscription table and a user table. I need to combine the two to get all the emails, since it's possible to create an account without subscribing and vice versa. Easy enough so far:

SELECT email FROM emailcapture
UNION
SELECT email FROM cpnc_User

Now, this gets me the complete list of all emails. For each email on this combined list, I need to add an extra piece of information: the created date. Both emailcapture and cpnc_User tables have a "created" field. The created date should be the earlier of the two dates, if both dates exist开发者_StackOverflow中文版, or, if only one exists and the other is NULL, it should just be the one that exists.

How can I change this query so that it returns this extra piece of information, the created date? Keep in mind that the new query I seek should return exactly the same number of rows as the query above.

Thanks, Jonah


SELECT i.email, MIN(i.date_creation) FROM
(SELECT email, date_creation FROM emailcapture
UNION ALL
SELECT email, date_creation FROM cpnc_User) as InnerTable i
GROUP BY i.email 
0

精彩评论

暂无评论...
验证码 换一张
取 消