如何比较where子句中的多行

时间:2017-03-01 02:28:47

标签: mysql sql database postgresql

Given table:

create table borrower (
customer_number char ( 1 0 ) ,
loan_number char ( 1 0 ));

问题:找到与客户“1234”共享的所有贷款的每个客户的客户编号(即ID)。如果客户编号为“1234”的客户有贷款“L1”和“L2”,则需要找到共享“L1”和“L2”的所有客户(包括客户“1234”)。

我在表中创建了4个条目(customer_number,loan_number)是(1234,L1)(1234,L2)(1,LI)(1,L2)(2,L1)。我正在使用查询

select distinct customer_number 
from borrower
where loan_number IN(
    select loan_number 
    from borrower 
    where customer_number='1234'

但它正在检索输出,例如

1234, 1, 2 

我的输出中只需要1和1234(Customer_number),因为它是唯一同时具有贷款编号L1和L2的人。 Cutsomer_number" 2"与loan_number没有关联,所以它应该在输出中。

我也试过"所有"取代" IN"。

select distinct customer_number 
    from borrower
    where loan_number= all(
        select loan_number 
        from borrower 
        where customer_number='1234'

输出:没有

5 个答案:

答案 0 :(得分:2)

编辑:根据OP的澄清

SELECT customer_number
FROM borrower b1
    WHERE b1.loan_number IN
    (SELECT loan_number FROM borrower
     WHERE customer_number=1234
     )
GROUP BY customer_number
HAVING count(*)=
  (SELECT count(*)
   FROM borrower
   WHERE customer_number=1234)

说明:首先,我计算给定customer_number的贷款数量。在这种情况下,它应该是2。现在我将这个计数与计数相同的每组customer_number进行比较。

答案 1 :(得分:1)

我认为你只想要拥有贷款L1和L2的customer_number。所以你为你的表尝试这个代码

查询

SELECT customer_number
FROM
  (SELECT customer_number,
          loan_number
   FROM borrower
   WHERE loan_number IN (SELECT loan_number FROM borrower
 WHERE customer_number=1234)
   GROUP BY loan_number,
            customer_number)t
GROUP BY customer_number
HAVING count(*) =
  (SELECT count(*)
   FROM borrower
   WHERE customer_number=1234)

如果未提及客户编号。但我们需要客户名单,这些名单都适用于所有贷款,如L1和L2

查询2 -


SELECT customer_number
FROM
  (SELECT customer_number,
          loan_number
   FROM borrower
   WHERE loan_number IN (SELECT distinct(loan_number) FROM borrower)
   GROUP BY loan_number,
            customer_number)t
GROUP BY customer_number
HAVING count(*) =
  (SELECT count(distinct(loan_number)) FROM borrower)

Query2的说明

SELECT distinct(loan_number) FROM borrower

loan_number
L1        
L2        

以上查询用于从借款人表中查找唯一贷款。

SELECT customer_number, loan_number FROM borrower
   WHERE loan_number IN (SELECT loan_number FROM borrower
 WHERE customer_number=1234)
   GROUP BY loan_number, customer_number
customer_number    loan_number
1                   L1        
1234                L1        
2                   L1        
1                   L2        
1234                L2        

以上查询用于查找根据贷款进行贷款分组的客户。

SELECT count(distinct(loan_number)) FROM borrower

上面的代码给出了计数2作为输出。(即L1和L2)。在查询中有count(*)将检查customer_number将呈现多少次。从这个例子中,它打印出customer_number,他们都在表中呈现两次。

最后,Query2显示输出,如

1         
1234      

最终输出删除customer_number 2,因为他只有“L1”。用户1和1234都有贷款“L1”和“L2”

希望这段代码可以帮到你。

答案 2 :(得分:1)

你确定你执行了所有sql sentense吗?看起来很好:

select distinct customer_number 
from borrower
where 
  loan_number IN(
    select a.loan_number from borrower a where a.customer_number='1234'
  )

答案 3 :(得分:1)

您可以使用临时表和连接。

/* create a table of the loans we are looking for */
create table Temp select distinct loan_number from Borrower 
where customer_number = '1234';

/* join against the temp table and select borrowers with 3 matches */
select customer_number as 'Match' 
from (select customer_number, COUNT(*) as C 
         from Borrower 
         inner join Temp on Borrower.loan_number = Temp.loan_number
         group by customer_number) X
where C = 3

答案 4 :(得分:0)

我有另一个查询来检索所需的行,由于EXCEPT

,此查询可能无法在MySQL中运行
Select customer_number from borrower as b
where not exists
   (select loan_number from borrower where customer_number='1234')
except
   (select loan_number from borrower where customer_number=b.customer_number)