码迷,mamicode.com
首页 > 数据库 > 详细

MySql联合查询Union

时间:2015-05-14 20:12:59      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

记一条很长的sql,为了方便阅读,我已经加了段落:

 SELECT * FROM (  
 (SELECT 1 AS acheck, s1.id, s1.plan_name, s1.cost_mode, s1.sta_type, s1.cost_type, s1.cost_period_time, s1.cost_period_value, s1.`carrier_operator`,s1.cost_settle_type  
 FROM wo_serviceplan s1  JOIN wo_serviceplan_hotspot sh ON s1.id = sh.serviceplan_id  
 WHERE s1.is_enable = 1 AND sh.hotspot_id = 109 ORDER BY s1.plan_name ASC )  
 UNION ALL  
 (SELECT 2 AS acheck, s2.id, s2.plan_name, s2.cost_mode, s2.sta_type, s2.cost_type, s2.cost_period_time, s2.cost_period_value, s2.`carrier_operator`,s2.cost_settle_type  
 FROM wo_serviceplan s2  
 WHERE s2.is_enable = 1 AND s2.id NOT IN (  SELECT serviceplan_id FROM wo_serviceplan_hotspot WHERE 1=1  AND hotspot_id = 109 ) 
 ORDER BY s2.plan_name ASC ) ) AS s3   ORDER BY s3.acheck ASC, s3.plan_name ASC  LIMIT 0,20

MySql联合查询Union

标签:

原文地址:http://www.cnblogs.com/gyhkevin/p/4504193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!