关于数据库查询

[复制链接]
查看11 | 回复2 | 2012-11-24 18:40:53 | 显示全部楼层 |阅读模式
这种就是 两表链接的, 简单,多看看书, 多想想就通了,如下: t1 表中 和 t1id 和t2表中的t1id有对应关系,也就是主键和外键的对应,看到没,你自己看,t1中和t2中对应的只有一行数据。 也就是34不能包括进来, select t1.t1id,t1.name ,t2.no from t1,t2 where t1.t1id=t2.t1id; 你这样查出来只是一行数据,如下:t1id  name no1  1
张三 12
只包括一行数据,没有办法,只你条件中要把t2表中的no2=34也要包括进来。所以要进行套起来。 select t1id,name,(select no from t2 w...
回复

使用道具 举报

千问 | 2012-11-24 18:40:53 | 显示全部楼层
select t1id,name,(select no from t2 where t1.t1id = t2.t1id and t2id = 1) as no1,(select no from t2 where t1.t1id = t2.t1id and t2id = 2) as no2from t1...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行