聯合查詢union注意事項

SELECT * FROM   (select id,gdname,restricted_purchase,original_price,price,pic,text,seckill,seckill_start,seckill_end,roof_placement,number from yh_goods 
                    where 
                        seckill_start<= '1586241657' 
                    and seckill_end >= '1586241657' 
                    and `del` = 0 
                    AND `status` = 1 
                    AND `seckill` = 1 
                    AND `roof_placement` = 1  
                    order by roof_placement asc,update_time desc limit 9999999999) as a
                union 
                (select id,gdname,restricted_purchase,original_price,price,pic,text,seckill,seckill_start,seckill_end,roof_placement,number from yh_goods 
                    where 
                        seckill_start<= '1586241657' 
                    and seckill_end >= '1586241657' 
                    and `del` = 0 
                    AND `status` = 1 
                    AND `seckill` = 1 
                    AND `roof_placement` != 1 
                    order by seckill_end asc,create_time desc limit 9999999999) as b
                union 
                (select id,gdname,restricted_purchase,original_price,price,pic,text,seckill,seckill_start,seckill_end,roof_placement,number from yh_goods 
                    where 
                         `del` = 0 
                    AND `status` = 1 
                    AND `seckill` = 1 
                    order by seckill_end desc,create_time desc limit 9999999999
                )   limit 0,20

1、在使用union的子查詢的時候只能給第一個取別名  其他的取別名會報語法錯誤

2、在子查詢排序的時候  如果不寫limit的話會過濾掉排序  因爲寫上limit 9999999方可解決

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章