简体   繁体   English

如何比较where子句中的多行

[英]how to compare multiple rows in where clause

Given table:

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

question: Find the customer number (ie, ID) of every customer that shares all of the loans that customer “1234” has. 问题:找出共享“ 1234”客户所有贷款的每个客户的客户编号(即ID)。 If the customer whose customer number is “1234” has loans “L1” and “L2”, you need to find all customers (including customer “1234”) that share both “L1” and “L2”. 如果客户编号为“ 1234”的客户有贷款“ L1”和“ L2”,则需要查找共享“ L1”和“ L2”的所有客户(包括客户“ 1234”)。

I have created 4 entries in the table for the columns (customer_number, loan_number) are (1234, L1) (1234, L2) (1, LI)(1, L2)(2, L1). 我在表中为(customer_number,loan_number)列创建了4个条目,分别为(1234,L1)(1234,L2)(1,LI)(1,L2)(2,L1)。 I am using query 我正在使用查询

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

but it is retrieving output such as 但它正在检索诸如

1234, 1, 2 

I want only 1 and 1234(Customer_number) in my output because it is the only one having both loan number L1 and L2. 我只想在输出中输入1和1234(Customer_number),因为它是唯一同时具有借贷号L1和L2的那个。 Cutsomer_number "2" is not associated with both the loan_number, so it should come in the out put. Cutsomer_number“ 2”既不与loan_number都关联,因此应该输入输出。

I have also tried "all" in place of "IN". 我也尝试过“全部”代替“ IN”。

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

output: nothing 输出:无

Edit: As per OP's clarification 编辑:根据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)

Explanation: First I am counting the number of loans for the given customer_number. 说明:首先,我计算给定的customer_number的贷款数量。 In this case, it should be 2 . 在这种情况下,应该为2 Now I am comparing this count with each group of customer_number where the count is same. 现在,我将此计数与计数相同的每组customer_number进行比较。

I think you want only customer_number who have both loan L1 and L2. 我认为您只需要同时拥有贷款L1和L2的customer_number。 so you try this code for your table 所以你为你的表尝试这个代码

Query - 查询-

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)

If customer number not mentioned. 如果客户编号未提及。 But we need the customer list who are all applicable for all the loans like both L1 and L2 但是我们需要一个客户清单,这些清单都适用于所有贷款,例如L1和L2

Query2 - 查询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)

Explanation for Query2 Query2的说明

SELECT distinct(loan_number) FROM borrower

loan_number
L1        
L2

The above query is used to find unique loans from borrower table. 上面的查询用于从借款人表中查找唯一的贷款。

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

This above query is used to find the customers who are all having the loans grouping based on their loans. 上面的查询用于查找所有根据其贷款进行贷款分组的客户。

SELECT count(distinct(loan_number)) FROM borrower

The above code gives count 2 as output.(ie, L1 and L2). 上面的代码将计数2作为输出(即L1和L2)。 In Query Having count(*) will check how many times customer_number will present. 在具有计数(*)的查询中,将检查customer_number出现的次数。 From this example, it prints customer_number who are all presenting two times in table. 从这个例子中,它打印出customer_number,它们都在表中显示两次。

Finally Query2 shows the output like 最终Query2显示如下输出

1         
1234

Final output remove customer_number 2 because he has only "L1". 最终输出删除customer_number 2,因为他只有“ L1”。 Both user 1 and 1234 has both loans "L1" and "L2" 用户1和1234都有贷款“ L1”和“ L2”

Hope this code will helps you. 希望这段代码对您有所帮助。

Are you sure that you execute all the sql sentense? 您确定执行所有sql sentense吗? looks like its ok: 看起来还可以:

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

You could use a temporary table and a join. 您可以使用临时表和联接。

/* 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

I got another query to retrieve desired rows, this query may not work in MySQL because of EXCEPT 我得到另一个查询来检索所需的行,由于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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM