我有2个返回结果的sql查询,都包含合同编号,现在我想获取合同编号的唯一值

这是查询

(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)


因此,例如,如果在联合之前的第一个查询返回了两个合同号为10的结果,而在联合之后的sql查询也返回了10,那么我们总共有3行,但是由于所有三行的合同号都是10,我需要只返回一行,这可能吗?

最佳答案

您可以将当前请求放在临时表中,然后在此表上执行选择:

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


(但是,您确定无法对联接做您想做的事吗?)

09-09 20:20