MySQL联盟+限制

时间:2012-03-17 10:03:22

标签: mysql sql

这是我的查询

select PhoneNumber as _data,PhoneType as _type from contact_phonenumbers where ContactID = 3 and Type = 'R' and PT = 'M' 
union 
SELECT EmailID,Type  from contact_emails where ContactID = 3 and Type = 'P' 
UNION
SELECT EmailID,Type from contact_emails where ContactID = 3 and Type = 'O' 

这里我的问题是我需要从每个select语句中只获取一行。是否可以通过“限制”获取它。

1 个答案:

答案 0 :(得分:5)

根据MySQL网站,您可以将限制应用于个人选择

(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);

http://dev.mysql.com/doc/refman/5.0/en/union.html