sql 多表联查询

假设a表
id b c
1 21 73
2 21 71
3 23 73

假设b表
id b c
1 65 66
2 65 66
3 65 66
要求:
1、查询a表 中b字段等于21的id
2、取出b表中id符合1条件的
怎么用一句sql表达出来?

可以用谓词或联结实现:

连接实现:
select * from b join a on b.id=a.id where a.b=21
联结实现的条件是两表id来自同一值域,表示意义相同。在连接时其实两可以作成一个表的:
也就是
id,a.b,a.c,b.b.b.c
但由于空值的问题,导致了部分依赖所以才会拆分成两个表的。

使用谓词实现:
select * from b where id in (select id from a where a.b=21)
这个可以实现两表id来自同一值域,但表示意义不同的情况。也就是说两表中的id有无关性。

相比较而言,连接的方式更快一些,但这种情况是两表来自同一值域,且意义相同,如果不是这种情况,可能得不到你正确的值的。而使用谓词不管意义是否相同,都可以得到正确的值。

玩数据库必须知道这两个表是否具有相关性,也就是设计时的意义,否则优化词句什么的都没有办法去做的!追问

刚才增加了20分
select * from b join a on b.id=a.id where a.b=21
如果A表中 b字段中的值与B表中 b字段中至少前部份相符的话,如何写?
比如 a表中 b字段 为9955878 B表中b字段为 995587854564这就是相符的。因为B表中b字段前面含有9955878,这样的字段类似查询语句如何写?
解了,我再增加30分。

追答

这个可以使用SQL中的函数去解决一下!如:

select * from b jion a on a.id=b.id where a.b = left(b.b,len(a.b))
当然也可以用其他的函数进行组合。这个意思只要让你明白要用函数解决的句式而已!但实现钶能使用多种方式,再如这种也是一样的:
select * from b jion a on a.id=b.id where a.b = cast(b.b as nvarchar(len(a.b)))
意思就是截击b.b与a.b一样长时其是否相等!

当然实现方式有很多,这个要熟悉SQL系统函数才行的。
如果全部是这种方式的话,我估计你设计的表有问题的,在数据库设计中我们一般都是避免这种重复的,如果都是这样,可能是存在一个决定关系,也就是存在一个对就应的一对多表关系并没有设计上来,以致于出现这种情况的!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-03-30

可以用谓词或联结实现:

连接实现:

select * from b join a on b.id=a.id where a.b=21

联结实现的条件是两表id来自同一值域,表示意义相同.在连接时其实两可以作成一个表的:

也就是

id,a.b,a.c,b.b.b.c

但由于空值的问题,导致了部分依赖所以才会拆分成两个表的.

使用谓词实现:

select * from b where id in (select id from a where a.b=21)

这个可以实现两表id来自同一值域,但表示意义不同的情况.也就是说两表中的id有无关性.

相比较而言,连接的方式更快一些,但这种情况是两表来自同一值域,且意义相同,如果不是这种情况,可能得不到你正确的值的.而使用谓词不管意义是否相同,都可以得到正确的值.

玩数据库必须知道这两个表是否具有相关性,也就是设计时的意义,否则优化词句什么的都没有办法去做的!

    有几种方式可以实现你的这个需求.

    1. 使用表 关联

    SELECT * FROM 表2 JOIN 表1 ON ( 表2.ID = 表1.列1 );

    2. 使用 IN

    SELECT * FROM 表2 WHERE ID IN ( SELECT 列1 FROM 表1);

    3.使用 EXISTS

    SELECT * FROM 表2

    WHERE EXISTS ( SELECT 1 FROM 表1 WHERE 表2.ID = 表1.列1 );

    select * from t2 left join t1 on t2.ID = t1.列1 where t1需要啥条件 and t2需要啥条件

    select * from 表2 where 某列 in (select 列1 from 表1) and id=1

本回答被网友采纳
第2个回答  2010-10-21
select LineId,Id,Country from Domestic
union all
select LineId,Id,Country from Freedom
-- 联合查询Domestic,Freedom表的LineId,Id,Country all代表不去除重复
--功能:[SQL语句] UNION [SQL语句]将两个语句中选择的同一列中的不同的值筛选出来

SELECT<表1>.<列名> ,<表2><列名>FROM<表1>OUTER JOIN<表2> ON<表1>.<列>=表2>.<列名>
--功能:实现两个表的外连接

Select Domestic.LineId,Freedom.LineId from Domestic,Freedom where Domestic.Sames=Freedom.Sames
Select Domestic.LineId,Freedom.LineId FROM Domestic inner join Freedom on Freedom.Sames=Domestic.Sames
--功能:实现两个表的内连接 把Domestic,Freedom两个表用Domestic.Sames=Freedom.Sames关联起来显示Domestic.LineId,Freedom.LineId
第3个回答  2012-02-12
1、查询a表 中b字段等于21的id
select a.id from a where a.b=21
2、取出b表中id符合1条件的
一句sql
select * from b where b.id in(select a.id from a where a.b=21)
第4个回答  2014-12-18
select datanum,pay_type,datatime
from (select * from aut_user_recharge_record_1
union all select * from aut_user_recharge_record_2
union all select * from aut_user_recharge_record_3
union all select * from aut_user_recharge_record_4
union all select * from aut_user_recharge_record_5
union all select * from aut_user_recharge_record_6
union all select * from aut_user_recharge_record_7
union all select * from aut_user_recharge_record_8
union all select * from aut_user_recharge_record_9
union all select * from aut_user_recharge_record_10)
相似回答