从 Union 获得独特的价值

Get distinct value from Union

场景:

Table A
MAPPING_ID    USER_TYPE    MODIFIED_ON
--------------------------------------

Table B
MAPPING_ID    ITEM_TYPE    MODIFIED_ON
--------------------------------------

我想获取最后一分钟修改的记录[MAPPING_ID]

(SELECT mapping_id FROM A WHERE modified_on > date_sub(now(),interval 1 minute) 
UNION 
(SELECT mapping_id from B WHERE modified_on > date_sub(now(),interval 1 minute) 

但问题是,结果中可能重复了 mapping_id!如何在 UNION 上获得 distinct mapping_id?

UNION 默认删除重复行,除非你使用 UNION ALL 参见 http://dev.mysql.com/doc/refman/5.0/en/union.html

如果您想手动执行此操作:

SELECT DISTINCT * FROM (
(SELECT mapping_id FROM A WHERE modified_on > date_sub(now(),interval 1 minute) 
UNION 
(SELECT mapping_id from B WHERE modified_on > date_sub(now(),interval 1 minute) 
) subsub