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