使用单个查询通过连接两个表来检索多值属性,而不会导致 mysql 中的字段重复

发布于 2024-09-19 08:51:42 字数 1216 浏览 3 评论 0原文

表 1:
查询:创建表客户端( applicationNo int 主键, 名称 varchar(20) );

插入语句:插入客户端值 (1,'XYZ'),(1,'ABC'),(1,'DEF');

applicationNo   |   name  
    1           |   XYZ  
    2           |   ABC  
    3           |   DEF  

表 2:
查询:创建表客户端( applicationNo int, 电话没有 Bigint, 外键(applicationNo)引用客户端(applicationNo), 主键(应用号,电话号) );

插入phoneNO值(1,999999),(1,888888),(2,777777),(3,666666),(3,555555);

applicationNo | phoneNo
1             |   999999
1             |   888888
2             |   777777
3             |   666666
3             |   555555

我可以检索吗通过以获取以下输出的方式连接两个表来连接元组,但使用单个查询,我也使用 mysql 5.1

applicationNo |   name | phoneNo1 | phoneNo2 
1             |   XYZ  |  999999  |   88888
2             |   ABC  |  77777   |   Null
3             |   DEF  |  66666   |   555555

编辑:额外信息
我尝试使用这种称为 cross tab 的东西。但我无法在 case 语句中使用totalPhoneNo

SELECT applicationNo,count(phoneNo) as totalPhoneNo,  
SUM(CASE WHEN totalPhoneNo= 1 THEN phoneNO ELSE Null END) AS phoneNo1,  
SUM(CASE WHEN totalPhoneNO = 2 THEN phoneNo ELSE Null END) AS phoneNo2  
FROM phoneNO GROUP BY applicationNo;

Table 1 :
QUERY: Create table client (
applicationNo int primary key,
name varchar(20)
);

Insert statement: Insert into client values (1,'XYZ'),(1,'ABC'),(1,'DEF');

applicationNo   |   name  
    1           |   XYZ  
    2           |   ABC  
    3           |   DEF  

Table 2:
Query : Create table client (
applicationNo int,
phoneNo Bigint,
foreign key (applicationNo) references client (applicationNo),
primary key(applicationNO,phoneNo)
);

Insert into phoneNO values (1,999999),(1,888888),(2,777777),(3,666666),(3,555555);

applicationNo | phoneNo
1             |   999999
1             |   888888
2             |   777777
3             |   666666
3             |   555555

Can I retrieve the tuples by joining both the tables in such a way that get the following output, but using single query, also I'm using mysql 5.1

applicationNo |   name | phoneNo1 | phoneNo2 
1             |   XYZ  |  999999  |   88888
2             |   ABC  |  77777   |   Null
3             |   DEF  |  66666   |   555555

Edited : extra information
I tried using this something called cross tab .But I'm not able to use the totalPhoneNo inside the case statement

SELECT applicationNo,count(phoneNo) as totalPhoneNo,  
SUM(CASE WHEN totalPhoneNo= 1 THEN phoneNO ELSE Null END) AS phoneNo1,  
SUM(CASE WHEN totalPhoneNO = 2 THEN phoneNo ELSE Null END) AS phoneNo2  
FROM phoneNO GROUP BY applicationNo;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

平定天下 2024-09-26 08:52:00

使用带有限制子句的子选择。第一个子选择选择第一个电话,另一个子选择选择第二个电话。

select ApplicationNo,
(SELECT phoneno FROM phones where phones.applicationno=app.ApplicationNo order by phoneno LIMIT 0, 1)as phone1,
(SELECT phoneno  FROM phones where phones.applicationno=app.ApplicationNo order by phoneno LIMIT 1, 1)as phone2
from application app

use subselects with limit clauses. the first subselect picks the first phone the other the second.

select ApplicationNo,
(SELECT phoneno FROM phones where phones.applicationno=app.ApplicationNo order by phoneno LIMIT 0, 1)as phone1,
(SELECT phoneno  FROM phones where phones.applicationno=app.ApplicationNo order by phoneno LIMIT 1, 1)as phone2
from application app
作妖 2024-09-26 08:51:54

这是针对 MSSQL 的。这个转换效果好吗?

With phones (ApplicationNo, PhoneNo, Instance) as
(Select ApplicationNo, PhoneNo,
  Row_Number OVER (Partition By ApplicationNo) as RowNum)
Select client.ApplicationNo, client.Name, 
  p1.PhoneNo as phoneNo1, p2.PhoneNo as phoneNo2
From client
  Left Join phones p1 on client.ApplicationNo=p1.ApplicationNo as p1.RowNum=1
  Left Join phones p2 on client.ApplicationNo=p2.ApplicationNo as p2.RowNum=2

Here is is for MSSQL. Does this convert nicely?

With phones (ApplicationNo, PhoneNo, Instance) as
(Select ApplicationNo, PhoneNo,
  Row_Number OVER (Partition By ApplicationNo) as RowNum)
Select client.ApplicationNo, client.Name, 
  p1.PhoneNo as phoneNo1, p2.PhoneNo as phoneNo2
From client
  Left Join phones p1 on client.ApplicationNo=p1.ApplicationNo as p1.RowNum=1
  Left Join phones p2 on client.ApplicationNo=p2.ApplicationNo as p2.RowNum=2
但可醉心 2024-09-26 08:51:51

尝试:

select c.applicationNo, 
       max(c.name) name,
       max(p.phoneNo) phoneNo1,
       case 
           when max(p.phoneNo) = min(p.phoneNo) then NULL 
           else min(p.phoneNo) 
       end phoneNo2
from client c
left join phoneNo p on c.applicationNo = p.applicationNo
group by c.applicationNo

Try:

select c.applicationNo, 
       max(c.name) name,
       max(p.phoneNo) phoneNo1,
       case 
           when max(p.phoneNo) = min(p.phoneNo) then NULL 
           else min(p.phoneNo) 
       end phoneNo2
from client c
left join phoneNo p on c.applicationNo = p.applicationNo
group by c.applicationNo
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文