单个查询比同一个查询 UNION 另一个提供更多的结果?
Single query gives more result than same query UNION another?
我不确定我是否误解了一个基本的东西,或者我的数据库如何解释查询有问题... 运行 universe
当我运行
SELECT POIDSBRUT, PRIXBRUTU, FAMILLENU, TPSMOU, TPSMOUPLANIF, QTE1PLANIFMOU, OF5
FROM GPSOF
WHERE DFINPLANIFMOU >= '%1' AND DFINPLANIFMOU <= '%2' AND EQPLANIFMOU <> 'A placer' AND EQPLANIFMOU LIKE 'A%'
它给了我 5 个结果(这里一切正常)。
但是当我运行
SELECT POIDSBRUT, PRIXBRUTU, FAMILLENU, TPSMOU, TPSMOUPLANIF, QTE1PLANIFMOU, OF5
FROM GPSOF
WHERE DFINPLANIFMOU >= '%1' AND DFINPLANIFMOU <= '%2' AND EQPLANIFMOU <> 'A placer' AND EQPLANIFMOU LIKE 'A%'
UNION
SELECT POIDSBRUT, PRIXBRUTU, FAMILLENU, TPSMOU, TPSMOU2PLANIF, QTE2PLANIFMOU, OF5
FROM GPSOF
WHERE DFIN2PLANIFMOU >= '%1' AND DFIN2PLANIFMOU <= '%2' AND EQ2PLANIFMOU <> 'A placer' AND EQ2PLANIFMOU LIKE 'A%'
UNION
SELECT POIDSBRUT, PRIXBRUTU, FAMILLENU, TPSMOU, TPSMOU3PLANIF, QTE3PLANIFMOU, OF5
FROM GPSOF
WHERE DFIN3PLANIFMOU >= '%1' AND DFIN3PLANIFMOU <= '%2' AND EQ3PLANIFMOU <> 'A placer' AND EQ3PLANIFMOU LIKE 'A%'
我只得到 2 个结果。要么我不明白 UNION 应该如何工作,要么我的数据库有另一个问题......
谢谢
UNION 删除重复项 - 我猜您的第一个结果集中有 4 条记录是相同的?
为什么要使用 union
进行此查询?为什么不只是 select 您想要的行?
SELECT POIDSBRUT, PRIXBRUTU, FAMILLENU, TPSMOU, TPSMOUPLANIF,
(CASE WHEN EQPLANIFMOU LIKE 'A%' THEN QTE1PLANIFMOU
WHEN EQPLAN2IFMOU LIKE 'A%' THEN QTE2PLANIFMOU
WHEN EQPLAN3IFMOU LIKE 'A%' THEN QTE3PLANIFMOU
END) as QTE1PLANIFMOU,
OF5
FROM GPSOF
WHERE DFINPLANIFMOU >= '%1' AND DFINPLANIFMOU <= '%2' AND EQPLANIFMOU <> 'A placer' AND
(EQPLANIFMOU LIKE 'A%' OR
EQPLAN2IFMOU LIKE 'A%' OR
EQPLAN3IFMOU LIKE 'A%'
)
A UNION statement effectively does a SELECT DISTINCT on the results set. If you know that all the records returned are unique from your union, use UNION ALL instead, it gives faster results.
我不确定我是否误解了一个基本的东西,或者我的数据库如何解释查询有问题... 运行 universe
当我运行
SELECT POIDSBRUT, PRIXBRUTU, FAMILLENU, TPSMOU, TPSMOUPLANIF, QTE1PLANIFMOU, OF5
FROM GPSOF
WHERE DFINPLANIFMOU >= '%1' AND DFINPLANIFMOU <= '%2' AND EQPLANIFMOU <> 'A placer' AND EQPLANIFMOU LIKE 'A%'
它给了我 5 个结果(这里一切正常)。
但是当我运行
SELECT POIDSBRUT, PRIXBRUTU, FAMILLENU, TPSMOU, TPSMOUPLANIF, QTE1PLANIFMOU, OF5
FROM GPSOF
WHERE DFINPLANIFMOU >= '%1' AND DFINPLANIFMOU <= '%2' AND EQPLANIFMOU <> 'A placer' AND EQPLANIFMOU LIKE 'A%'
UNION
SELECT POIDSBRUT, PRIXBRUTU, FAMILLENU, TPSMOU, TPSMOU2PLANIF, QTE2PLANIFMOU, OF5
FROM GPSOF
WHERE DFIN2PLANIFMOU >= '%1' AND DFIN2PLANIFMOU <= '%2' AND EQ2PLANIFMOU <> 'A placer' AND EQ2PLANIFMOU LIKE 'A%'
UNION
SELECT POIDSBRUT, PRIXBRUTU, FAMILLENU, TPSMOU, TPSMOU3PLANIF, QTE3PLANIFMOU, OF5
FROM GPSOF
WHERE DFIN3PLANIFMOU >= '%1' AND DFIN3PLANIFMOU <= '%2' AND EQ3PLANIFMOU <> 'A placer' AND EQ3PLANIFMOU LIKE 'A%'
我只得到 2 个结果。要么我不明白 UNION 应该如何工作,要么我的数据库有另一个问题...... 谢谢
UNION 删除重复项 - 我猜您的第一个结果集中有 4 条记录是相同的?
为什么要使用 union
进行此查询?为什么不只是 select 您想要的行?
SELECT POIDSBRUT, PRIXBRUTU, FAMILLENU, TPSMOU, TPSMOUPLANIF,
(CASE WHEN EQPLANIFMOU LIKE 'A%' THEN QTE1PLANIFMOU
WHEN EQPLAN2IFMOU LIKE 'A%' THEN QTE2PLANIFMOU
WHEN EQPLAN3IFMOU LIKE 'A%' THEN QTE3PLANIFMOU
END) as QTE1PLANIFMOU,
OF5
FROM GPSOF
WHERE DFINPLANIFMOU >= '%1' AND DFINPLANIFMOU <= '%2' AND EQPLANIFMOU <> 'A placer' AND
(EQPLANIFMOU LIKE 'A%' OR
EQPLAN2IFMOU LIKE 'A%' OR
EQPLAN3IFMOU LIKE 'A%'
)
A UNION statement effectively does a SELECT DISTINCT on the results set. If you know that all the records returned are unique from your union, use UNION ALL instead, it gives faster results.