首页 > 数据库技术 > 详细

MySql联合查询Union

时间:2015-05-14 20:12:05      阅读:256      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!