将两个单独的SQL查询合并为一个

时间:2014-04-23 10:24:57

标签: mysql sql

我可能遇到一些不太困难的SQL任务有困难。

我有两个问题要集合在一起。它们都可以单独工作,但是当我尝试将它们组合在一起时,我会得到不同的错误,例如:

错误116:当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式。

与此同时,我不想使用 EXIST ,而是 IN

以下是查询:

首先:

  SELECT C.Id, C.Name, BC.Id AS BCID
    FROM Customers AS C
    RIGHT JOIN Bills AS Bc ON C.Id = BC.Bills_Customer
    RIGHT JOIN Months AS M ON Bc.Month_Bills = M.Id
    WHERE C.Argument = 'KP'
    AND YEAR(bm.Datum) = YEAR(CURRENT_TIMESTAMP) AND MONTH(bm.Datum) = MONTH(CURRENT_TIMESTAMP)
    ORDER BY C.Name

第二

  SELECT DISTINCT Account, LastLogin, Licences_Bills 
    FROM Licences 
    WHERE LastLogin > CONVERT(varchar,dateadd(d,-(day(dateadd(m,-1,getdate()-2))),dateadd(m,-1,getdate()-1)),106)
    AND LastLogin < CONVERT(varchar,dateadd(d,-(day(getdate())),getdate()),106)
    AND Access = 1 --AND Licences_Bills IN
    ORDER BY Licences_Bills ASC

两个查询的结果如下:

首先:

 +----------+---------+-----------+
 |  Id      | Name    |   BCID    |  
 +----------+---------+-----------+
 |    1     |  John   |    500    |  
 +----------+---------+-----------+
 |    2     |  Max    |    501    |
 +----------+---------+-----------+
 |    5     |  Foo    |    502    |  
 +----------+---------+-----------+
 |    7     |  Bar    |    503    |
 +----------+---------+-----------+

第二

 +----------+--------------+-------------------+
 |  Account |   LastLogin  |  Licences_Bills   |  
 +----------+--------------+-------------------+
 |    abc   |  07.03.2014  |    500            |  
 +----------+--------------+-------------------+
 |    aac   |  13.03.2014  |    500            |
 +----------+--------------+-------------------+
 |    acb   |  28.03.2014  |    504            |  
 +----------+--------------+-------------------+
 |    bca   |  19.03.2014  |    506            |
 +----------+--------------+-------------------+

现在我想将这两者结合起来,以便它只显示第一个查询中的 BCID 和第二个查询中的 Licences_Bills 匹配的行。 / p>

我尝试使用 IN 然后进行嵌套选择,但没有成功。对我做错了什么的想法?一些有价值的资源的线索和/或链接也很受欢迎!

修改 最后,我想让第二个结果列表限于第一个查询中的那些账单。这就是我尝试使用IN然后嵌套选择的原因。另一方面,INNER JOIN会将结果作为两个表的交集。

另外,我不希望看到第一个查询中的列(截至连接时会发生什么)。只是最终结果构成第二个查询。

3 个答案:

答案 0 :(得分:2)

SELECT * FROM

(

SELECT C.Id, C.Name, BC.Id AS BCID
    FROM Customers AS C
    RIGHT JOIN Bills AS Bc ON C.Id = BC.Bills_Customer
    RIGHT JOIN Months AS M ON Bc.Month_Bills = M.Id
    WHERE C.Argument = 'KP'
    AND YEAR(bm.Datum) = YEAR(CURRENT_TIMESTAMP) AND MONTH(bm.Datum) = MONTH(CURRENT_TIMESTAMP)


) as T1
INNER JOIN

(
SELECT DISTINCT Account, LastLogin, Licences_Bills 
    FROM Licences 
    WHERE LastLogin > CONVERT(varchar,dateadd(d,-(day(dateadd(m,-1,getdate()-2))),dateadd(m,-1,getdate()-1)),106)
    AND LastLogin < CONVERT(varchar,dateadd(d,-(day(getdate())),getdate()),106)
    AND Access = 1

) T2

ON T1.BCID=T2.Licences_Bills

答案 1 :(得分:1)

尝试加入

SELECT t1.*,t2.*
FROM
(query 1) t1
JOIN (query 2) t2 
ON(t1.BCID = t2.Licences_Bills)

答案 2 :(得分:1)

要将第二个结果列表限制为您在第一个查询中找到的帐单,您必须将它们与IN或EXISTS结合使用。我不知道为什么这对你不起作用。也许只是一个错字?以下应该有效。它简单地将两个语句与IN组合在一起。所以在内部查询中我只选择BC.ID并删除order by子句。

SELECT DISTINCT Account, LastLogin, Licences_Bills 
FROM Licences 
WHERE LastLogin > CONVERT(varchar,dateadd(d,-(day(dateadd(m,-1,getdate()-2))),dateadd(m,-1,getdate()-1)),106)
AND LastLogin < CONVERT(varchar,dateadd(d,-(day(getdate())),getdate()),106)
AND Access = 1 
AND Licences_Bills IN
(
  SELECT BC.Id
  FROM Customers AS C
  RIGHT JOIN Bills AS Bc ON C.Id = BC.Bills_Customer
  RIGHT JOIN Months AS M ON Bc.Month_Bills = M.Id
  WHERE C.Argument = 'KP'
  AND YEAR(bm.Datum) = YEAR(CURRENT_TIMESTAMP) AND MONTH(bm.Datum) = MONTH(CURRENT_TIMESTAMP)
)
ORDER BY Licences_Bills ASC