MYSQL中的UNION
UNION在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果。
举例说明:
select * from table1 union select * from table2
这个SQL在运行时先取出两个表的结果,再用排序空间进行排序删除重复的记录,最后返回结果集,如果表数据量大的话可能会导致用磁盘进行排序。
MySQL中的UNION ALL UNION ALL只是简单的将两个结果合并后就返回。这样,如果返回的两个结果集中有重复的数据,那么返回的结果集就会包含重复的数据了。
举例说明:
select * from table1 union all select * from table2
注: 使用 UNION 时 前一个 select column的个数要等于后一个select column的个数 如: table1: (id,createDate,lastUpdateDate,desc,num,hashCode), table2: (id,createDate,lastUpdateDate,desc)
如果现在使用: select * from table1 UNION ALL select * from table2 则是不会成功的, 数据库为报: Error The used SELECT statements have a different number of columns
这是提示查询的两张表的字段不统一,如果table1比table2的字段内容多,可以使用空字符串来代替
select id,createDate,lastUpdateDate,desc,num,hashCode from table1 UNION ALL select id,createDate,lastUpdateDate,desc,'','' from table2
如果里面有不想要的,千万要记住前面查询内容要和后面查询内容的字段个数要一样,前面你查询4个,后面也要相应的放4个,这样就不会提示参数数量不同的错误了。mysql中UNION ALL用法-LMLPHP
其实稍稍修改一下就可以了 对于 'select id,createDate,desc,hasCode from table1' 可以任意选择4个field
从效率上说,UNION ALL 要比UNION快很多,所以,如果可以确认合并的两个结果集中不包含重复的数据的话,那么就使用UNION ALL。
如果遇到两张表数据不同来集合查询,可以使用union all这个函数进行操作
SELECT COUNT(c.a) FROM ( (SELECT UID a,ID,SERIAL,ParkName,CardNO,ScoreRealPay,PayFlag,PayType,Createtime FROM cp_consumption_record WHERE UID=761 AND PayFlag=1 ORDER BY Createtime DESC) UNION ALL (SELECT UID a,CpResID,CpParkID,ParkSERIAL,CarCode,Price,BusinessType,CardNO,CreateDate FROM cp_reservation WHERE UID=761 AND BusinessType IN(1,2,3) ORDER BY CreateDate DESC) ) c
这是查询结果集共有多少条数据,
如果还有查询条件,直接在c后面添加就可以,比如按照时间进行查询
SELECT c.UID,c.ScoreRealPay,c.PayFlag,c.PayType FROM ( (SELECT UID AS UID,ID AS ID,SERIAL AS SERIAL ,ParkName AS ParkName,CardNO CardNO,ScoreRealPay ScoreRealPay,PayFlag PayFlag,PayType PayType,Createtime Createtime FROM cp_consumption_record WHERE UID=761 AND PayFlag=1 ORDER BY Createtime DESC) UNION ALL (SELECT UID a,CpResID,CpParkID,ParkSERIAL,CarCode,Price,BusinessType,CardNO,CreateDate FROM cp_reservation WHERE UID=761 AND BusinessType IN(1,2,3) ORDER BY CreateDate DESC) ) c ORDER BY Createtime DESC/ASC
这里强调一下,你要按照什么样的条件进行查询时,要分别在select子查询中添加上条件,最后在按照统一的时间倒序或者正序
SELECT d.color_id,d.properties_id,
d.place_id,d.pic_front,
d.name,
SUM(d.qa_time) AS qaTime,
SUM(d.patter_time) AS patterTime,
SUM(d.pmaker_time) AS pmakerTime,
SUM(d.nuclear_time) AS nuclearTime,
SUM(d.transport_time) AS transportTime,
SUM(d.tailor_time) AS tailorTime,
SUM(d.batcher_time) AS batcherTime,
SUM(d.proof_alloc_time) AS proof_allocTime,
SUM(d.proof_time) AS proofTime,
SUM(d.quality_time) AS qualityTime,
SUM(d.review_time) AS reviewTime
FROM
((SELECT a.color_id,a.properties_id,a.flag,a.noticecode
,
c.place_id,c.pic_front,
d.name,
b.qa_time,
b.patter_time,
b.pmaker_time,
b.nuclear_time,
b.transport_time,
b.tailor_time,
b.batcher_time,
b.proof_alloc_time,
b.proof_time,
b.quality_time,
b.review_time
FROM proofing_notice a INNER JOIN proofing_notice_nodedate b ON a.id = b.notice_id AND a.STATUS<>0 AND a.deleted_at IS NULL AND a.flag=0
AND a.paper_type_id=0 AND a.delivery_date IS NULL AND a.nuclear_date BETWEEN '2023-06-01' and '2023-06-30'
LEFT JOIN deve_color c ON a.color_id = c.id
LEFT JOIN basic_config d ON c.place_id = d.id
UNION ALL
SELECT a.color_id,a.properties_id,a.flag,a.noticecode
,
c.place_id,c.pic_front,
d.name,
b.qa_time,
b.patter_time,
b.pmaker_time,
b.nuclear_time,
b.transport_time,
b.tailor_time,
b.batcher_time,
b.proof_alloc_time,
b.proof_time,
b.quality_time,
b.review_time
FROM proofing_notice a INNER JOIN proofing_notice_nodedate b ON a.id = b.notice_id AND a.STATUS<>0 AND a.deleted_at IS NULL AND a.flag=0
AND a.paper_type_id=1 AND a.delivery_date IS NULL AND a.transport_date BETWEEN '2023-06-01' and '2023-06-30'
LEFT JOIN deve_color c ON a.color_id = c.id
LEFT JOIN basic_config d ON c.place_id = d.id
UNION ALL
SELECT a.color_id,a.properties_id,a.flag,a.noticecode
,
c.place_id,c.pic_front,
d.name,
b.qa_time,
b.patter_time,
b.pmaker_time,
b.nuclear_time,
b.transport_time,
b.tailor_time,
b.batcher_time,
b.proof_alloc_time,
b.proof_time,
b.quality_time,
b.review_time
FROM proofing_notice a INNER JOIN proofing_notice_nodedate b ON a.id = b.notice_id AND a.STATUS<>0 AND a.deleted_at IS NULL AND a.flag=0
AND a.delivery_date IS NOT NULL AND a.review_date BETWEEN '2023-06-01' and '2023-06-30'
LEFT JOIN deve_color c ON a.color_id = c.id
LEFT JOIN basic_config d ON c.place_id = d.id
UNION ALL
SELECT a.color_id,a.properties_id,a.flag,a.noticecode
,
c.place_id,c.pic_front,
d.name,
b.qa_time,
b.patter_time,
b.pmaker_time,
b.nuclear_time,
b.transport_time,
b.tailor_time,
b.batcher_time,
b.proof_alloc_time,
b.proof_time,
b.quality_time,
b.review_time
FROM proofing_notice a INNER JOIN proofing_notice_nodedate b ON a.id = b.notice_id AND a.STATUS<>0 AND a.deleted_at IS NULL AND a.flag=1
AND a.paper_type_id=0 AND a.delivery_date IS NULL AND a.nuclear_date BETWEEN '2023-06-01' and '2023-06-30'
LEFT JOIN pro_large.large_color c ON a.color_id = c.id
LEFT JOIN basic_config d ON c.place_id = d.id
UNION ALL
SELECT a.color_id,a.properties_id,a.flag,a.noticecode
,
c.place_id,c.pic_front,
d.name,
b.qa_time,
b.patter_time,
b.pmaker_time,
b.nuclear_time,
b.transport_time,
b.tailor_time,
b.batcher_time,
b.proof_alloc_time,
b.proof_time,
b.quality_time,
b.review_time
FROM proofing_notice a INNER JOIN proofing_notice_nodedate b ON a.id = b.notice_id AND a.STATUS<>0 AND a.deleted_at IS NULL AND a.flag=1
AND a.paper_type_id=1 AND a.delivery_date IS NULL AND a.transport_date BETWEEN '2023-06-01' and '2023-06-30'
LEFT JOIN pro_large.large_color c ON a.color_id = c.id
LEFT JOIN basic_config d ON c.place_id = d.id
UNION ALL
SELECT a.color_id,a.properties_id,a.flag,a.noticecode
,
c.place_id,c.pic_front,
d.name,
b.qa_time,
b.patter_time,
b.pmaker_time,
b.nuclear_time,
b.transport_time,
b.tailor_time,
b.batcher_time,
b.proof_alloc_time,
b.proof_time,
b.quality_time,
b.review_time
FROM proofing_notice a INNER JOIN proofing_notice_nodedate b ON a.id = b.notice_id AND a.STATUS<>0 AND a.deleted_at IS NULL AND a.flag=1
AND a.delivery_date IS NOT NULL AND a.review_date BETWEEN '2023-06-01' and '2023-06-30'
LEFT JOIN pro_large.large_color c ON a.color_id = c.id
LEFT JOIN basic_config d ON c.place_id = d.id
)) AS d
GROUP BY d.color_id,d.properties_id
ORDER BY d.color_id,d.properties_id