gpt4 book ai didi

mysql - sql连接同一个表中的2行

转载 作者:可可西里 更新时间:2023-11-01 06:35:16 24 4
gpt4 key购买 nike

我有下表:

Name   Type     Value
---------------------
mike phone 123
mike address nyc
bob address nj
bob phone 333

我想要这样的结果:

name  value  value
-------------------
mike nyc 123
bob nj 333

我该怎么做?

最佳答案

它被称为自连接。诀窍是使用别名。

select 
address.name,
address.value as address,
phone.value as phone
from
yourtable as address left join
yourtable as phone on address.name = phone.name
where address.type = 'address' and
(phone.type is null or phone.type = 'phone')

查询假设每个名字都有一个地址,但电话号码是可选的。

关于mysql - sql连接同一个表中的2行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6614867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com