SQL1以下:sql
SELECT count(*) FROM ( SELECT id,product title, images,type , '凍品加工' categoryName, create_time createTime FROM sad_technology UNION SELECT id,CONCAT(start_city,'-',end_city) title, images,type ,'冷鏈物流' categoryName, create_time createTime FROM sad_transport UNION SELECT id,title, images, type,'冷鏈倉儲' categoryName, create_time createTime FROM sad_warehouse_emand UNION SELECT id,product_name title, images, type,'凍品交易' categoryName, create_time createTime FROM sad_transaction ) t WHERE t.type = 'SUPPLY' order by t.createTime desc
結果以下:結果有83條記錄
數據庫
執行SQL2,結果有73條記錄3d
SELECT count(*) FROM ( SELECT product title, images,type , '凍品加工' categoryName, create_time createTime FROM sad_technology UNION SELECT CONCAT(start_city,'-',end_city) title, images,type ,'冷鏈物流' categoryName, create_time createTime FROM sad_transport UNION SELECT title, images, type,'冷鏈倉儲' categoryName, create_time createTime FROM sad_warehouse_emand UNION SELECT product_name title, images, type,'凍品交易' categoryName, create_time createTime FROM sad_transaction ) t WHERE t.type = 'SUPPLY' ORDER BY createTime
很奇怪。仔細分析sql1和sql2的區別是sql1裏子查詢裏邊每一個表查詢多了個id字段
code
已知id爲每一個表的主鍵且在每一個表中都惟一且不爲空.結果爲啥不同嘞?blog
再補充兩張數據圖吧: 沒id的sql
有id的sql
排序
此時數據庫裏又多了幾天記錄。有order by的緣由是須要排序後limit數據。ci