我有几个联合UNION子句的查询
我想从一个字段中过滤总结果。
一种想法是过滤每个查询,但是这可能是“费力”的工作。

SELECT 'GROUP1' AS GROUP, LicenseCode, Field2, Field3, Field4
FROM TABLE
UNION
SELECT 'GROUP2' AS GROUP, LicenseCode, Field2, Field3, Field4
FROM TABLE
UNION
SELECT 'GROUP3' AS GROUP, LicenseCode, Field2, Field3, Field4
FROM TABLE

--WHERE LicenseCode > 7878


显然,最终声明不起作用。
如何过滤总的UNION结果? (无需在每个查询中放入语句(WHERE LicenseCode > 7878)。

谢谢!
问候

最佳答案

将联合作为子查询,然后过滤结果:

select group, LicenseCode, Field2, Field3, Field4
from (
  SELECT 'GROUP1' AS GROUP, LicenseCode, Field2, Field3, Field4
  FROM TABLE
  UNION
  SELECT 'GROUP2' AS GROUP, LicenseCode, Field2, Field3, Field4
  FROM TABLE
  UNION
  SELECT 'GROUP3' AS GROUP, LicenseCode, Field2, Field3, Field4
  FROM TABLE
) x
WHERE LicenseCode > 7878

10-04 21:35