select code , name , old from student a join user b on a.code=b.code
union all
select code , name , old from teacher a join user b on a.code=b.code
union all
select code , name , old from teacher a join user b on a.code=b.code
解决方案 »
- ★就这么简单的查询问题,走过路过的都近来看看啊
- sql 2008 远程过程调用失败 0x800706be
- 急求sql语句统计数结构数据,如下
- 请问,如何将检索结果,合并在一行
- 把Access数据库的表批量插入SQL库里
- [求助]单个触发器判断Insert/Delete/Update操作
- 请教个高手问题!关于SQL2000 CHAR的 问题!
- 行列转换问题
- 在插入数据的同时能否对数据排序
- SQL 如何将A表的列项动态新增到B表的列
- 求一个简单的sql语句
- 提问:jsp连数据库的测试时,甩出异常:com.microsoft.sqlserver.jdbc.SQLServerException: 。。。。。
where a.code=b.code and a.code=c.code
where a.code=b.code or a.code=c.code
where a.code=b.code or a.code=c.code
正解!
where a.code=b.code or a.code=c.code
得到的结果如下:code name name
S01 李强 张三
T01 李强 张三
S01 王芳 张三
T02 王芳 张三
S02 李强 李四
T01 李强 李四
S02 王芳 李四
T02 王芳 李四
create table student(code varchar(10),name varchar(20))
insert into student select 'S01','张三'
union all select 'S02','李四'create table teacher(code varchar(10),name varchar(20))
insert into teacher select 'T01','李强'
union all select
'T02','王芳'create table [user](code varchar(10),old int)
insert into [user] select 's01',21
union all select 's02',19
union all select 'T01',45
union all select 'T02',51--查询:
select a.code,b.name,a.old from [user] a ,(
select * from student
union all
select * from teacher
)b where a.code=b.code或者:
select a.code , name , old from student a join [user] b on a.code=b.code
union all
select a.code , name , old from teacher a join [user] b on a.code=b.code--结果:
s01 张三 21
s02 李四 19
T01 李强 45
T02 王芳 51
select a.*,b.name from [user] a join student b on a.code=b.code
union all
select a.*,b.name from [user] a join teacher b on a.code=b.code
where a.code=b.code or a.code=c.code--这种写法肯定错了,“b.name“那就是姓名都来自于
教师表了,怎么会显示学生的姓名呢?
比较简单的办法就是用楼上的
select a.code , name , old from student a join [user] b on a.code=b.code
union all
select a.code , name , old from teacher a join [user] b on a.code=b.code为正解