我有2个返回结果的sql查询,两个都包含合同号,现在我想得到合同号的唯一值
HEre是查询
(SELECT contractno, dsignoff FROM campaigns WHERE clientid = 20010490 AND contractno != '' GROUP BY contractno,dsignoff) UNION (SELECT id AS contractno,signoffdate AS dsignoff FROM contract_details WHERE clientid = 20010490)
因此,例如,如果union之前的第一个查询返回两个结果,合同号为10,并且union之后的sql查询也返回10,那么我们总共有3行,但是因为所有三行的contractno都是10 ,我只需要返回一行,这可能吗?
答案 0 :(得分:3)
您可以将当前请求放在临时表中,并在此表上执行select:
SELECT
*
FROM
(
/* Your request goes here */
SELECT
contractno,
dsignoff
FROM
campaigns
WHERE
clientid = 20010490 AND contractno != ''
GROUP BY
contractno,dsignoff
UNION
SELECT
id AS contractno,
signoffdate AS dsignoff
FROM
contract_details
WHERE clientid = 20010490)
) AS tmp
GROUP BY
tmp.contractno
(但你确定你不能用连接做你想做的事吗?)