怎么写sql语句可以实现这个功能

来源:百度知道 编辑:UC知道 时间:2024/05/10 07:22:41
说的简单一点,有2个表
A表是这个样子的
id name sex
1 aaa m
2 bbb f
3 ccc m

B表是这个样子的
id name value
1 address address1
1 age 20
2 address address2
2 age 21
3 address address3
3 age 22

问 我怎么写sql 可以得到以下结果

id name sex address age
1 aaa m address1 20
2 bbb f address2 21
3 ccc m address3 22
现在问题有变
如果a表中的内容是
id name sex
1 aaa m
2 bbb f
3 ccc m
4 ddd f
而b表中没有id=4的纪录
怎么能让4也被正常选出,而address和age两项显示none

需要使用子查询。
select a.id,a.name,a.sex,(select top 1 value from b where id=a.id and name='address') as address,(select top 1 value from b where id=a.id and name='age') as age
from a

兄弟伙/你是北大请鸟的吧?

id name sex
1 aaa m
2 bbb f
3 ccc m

id name value
1 address address1
1 age 20
2 address address2
2 age 21
3 address address3
3 age 22

SELECT * FROM 表1,表2 WHERE 表1.ID=表2.ID

rs("ID")+rs("Name")+rs("address")+rs("age")

select id,name,sex,address,age from a innor join b on a.name=b.name

看了一下三个人的答案,“沉默用户”应该是对的!