Ⅰ 例举几条多表查询sql语句
1.select a.学号,a.姓名,b.学籍号,a.性别,a.民族,a.身份证号,a.联系电话
from 学生表 a, 注册表 b
2.select count (*) [注册人数]from 注册表
where 注册时间='2010-10-20'(select 专业代码,专业名称,总金额 from 专业表,注册表,收据表
where 注册时间='2010-10-20')
3.select a.所属院系,b.性别,c.房间号,床位状态
from 专业表 a, 学生表 b,房间表,床位表 c
where 床位状态 = '空位'
Ⅱ sql的多表查询
Select
A.id,
A.id1,
B.name,
A.id2,
C.name,
A.id3,
C.name
from
A
left
join
B
ON
A.id1
=
B.id
left
join
C
ON
A.id2
=
C.id
left
join
C
ON
A.id3
=
C.id
order
by
....
说明:1。这是执行对ID匹配Name
的最普遍做法,注意上面的left
join中以A为源数据表来遍历,如果在B或C中找不到对应的name那么该name字段就显示为空。
2。不要担心
left
join
C
ON
A.id2
=
C.id
left
join
C
ON
A.id3
=
C.id
看起来挺奇怪,这其实很好理解,就相当于你将C表使用了两次来做匹配,你完全可以将C看成是一个D表来与A的id3再次匹配name值,只不过这个D表就还是使用你的C表而已。
Ⅲ sql联合查询语句(两张表)
sql联合查询语句(两张表)是:
select A.ID,A.VALUE,A.TYPE,A.NAME,B.KEY,B.ID,B.VALUE,B.NAME
min(VALUE),max(VALUE) from A left join B on A.ID = B.ID
where B.NAME="你输入的名字"
and B.VALUE > (select min(VALUE) from B where NAME="你输入的名字"))
and B.VALUE < (select min(VALUE) from B where NAME="你输入的名字"));
Ⅳ 在SQL中如何进行多表查询
要想多表查询,是有条件的。一般是几张表结构相似或者是有一样的ID号关联。例如:
select * from 表1,表2,表3 这是把3张表结果全部查出来
select * from 表1 where not exists(select 0 from 表2 where
表1.id=表2.id)
Ⅳ SQL多表连接查询实例分析(详细图文)
新建两张表:
表1:student
截图如下:
表2:course
截图如下:
(此时这样建表只是为了演示连接SQL语句,当然实际开发中我们不会这样建表,实际开发中这两个表会有自己不同的主键。)
一、外连接
外连接可分为:左连接、右连接、完全外连接。
1、左连接
left
join
或
left
outer
join
SQL语句:select
*
from
student
left
join
course
on
student.ID=course.ID
执行结果:
左外连接包含left
join左表所有行,如果左表中某行在右表没有匹配,则结果中对应行右表的部分全部为空(NULL).
注:此时我们不能说结果的行数等于左表数据的行数。当然此处查询结果的行数等于左表数据的行数,因为左右两表此时为一对一关系。
2、右连接
right
join
或
right
outer
join
SQL语句:select
*
from
student
right
join
course
on
student.ID=course.ID
执行结果:
右外连接包含right
join右表所有行,如果左表中某行在右表没有匹配,则结果中对应左表的部分全部为空(NULL)。
注:同样此时我们不能说结果的行数等于右表的行数。当然此处查询结果的行数等于左表数据的行数,因为左右两表此时为一对一关系。
3、完全外连接
full
join
或
full
outer
join
SQL语句:select
*
from
student
full
join
course
on
student.ID=course.ID
执行结果:
完全外连接包含full
join左右两表中所有的行,如果右表中某行在左表中没有匹配,则结果中对应行右表的部分全部为空(NULL),如果左表中某行在右表中没有匹配,则结果中对应行左表的部分全部为空(NULL)。
二、内连接
join 或
inner
join
SQL语句:select
*
from
student
inner
join
course
on
student.ID=course.ID
执行结果:
inner
join
是比较运算符,只返回符合条件的行。
此时相当于:select
*
from
student,course
where
student.ID=course.ID
三、交叉连接
cross
join
1.概念:没有
WHERE
子句的交叉联接将产生连接所涉及的表的笛卡尔积。第一个表的行数乘以第二个表的行数等于笛卡尔积结果集的大小。
SQL语句:select
*
from
student
cross
join
course
执行结果:
如果我们在此时给这条SQL加上WHERE子句的时候比如SQL:select
*
from
student
cross
join
course
where
student.ID=course.ID
此时将返回符合条件的结果集,结果和inner
join所示执行结果一样。
四、两表关系为一对多,多对一或多对多时的连接语句
当然上面两表为一对一关系,那么如果表A和表B为一对多、多对一或多对多的时候,我们又该如何写连接SQL语句呢?
其实两表一对多的SQL语句和一对一的SQL语句的写法都差不多,只是查询的结果不一样,当然两表也要略有改动。
比如表1的列可以改为:
Sno
Name
Cno
表2的列可以改为:
Cno
CName
这样两表就可以写一对多和多对一的SQL语句了,写法和上面的一对一SQL语句一样。
下面介绍一下当两表为多对多的时候我们该如何建表以及些SQL语句。
新建三表:
表A:
student
截图如下:
表B:
course
截图如下:
表C:
student_course
截图如下:
一个学生可以选择多门课程,一门课程可以被多个学生选择,因此学生表student和课程表course之间是多对多的关系。
当两表为多对多关系的时候,我们需要建立一个中间表student_course,中间表至少要有两表的主键,当然还可以有别的内容。
SQL语句:select
s.Name,C.Cname
from
student_course
as
sc
left
join
student
as
s
on
s.Sno=sc.Sno
left
join
course
as
c
on
c.Cno=sc.Cno
执行结果:
此条SQL执行的结果是学生选课的情况。
Ⅵ SQL server数据库里查询时新建查询怎么进行多表查询
首先要检查你的表与表之间是不是有约束(主外键约束),如果存在一般连接有左连接、右连接、内连接,下面给你举例:
----做笛卡尔积
select
s.id,s.name,sc.id,sc.sname,sc.score
from
infom
s
,score
sc
------内连接
写法一
select
s.id,s.name,sc.id,sc.sname,sc.score
from
infom
s
,score
sc
inner
join
score
sc
on
s.id=
sc.id
------内连接的条件
------on
s.id
<>sc.id
--------是全集
-
交集
------where
sc.score>80
------内连接
方法二
select
s.id,s.name,sc.id,sc.sname,sc.score
from
infom
s
,score
sc
where
s.id=
sc.id
-------------------------------------------------------外连接
左连接
--------------左表数据完全显示,右表中相同的数据显示,不同数据null
select
Student.name,score.score
from
Student
left
join
score
-----------------先写的为左表
on
Student.id=score
.id
-----------------连接条件
-------------------------------------------------------外连接
右连接
--------------右表数据完全显示,左表中相同的数据显示,不同数据显示null
select
Student.name,score.score
from
Student
right
join
score
on
Student.id=score
.id
-----------------------------------全连接
full
join
-----------------------------------左、右表的数据完全显示,相同的数据显示一次
select
Student.name,score.score
from
Student
full
join
score
on
Student.id=score
.id
-------------------------------交叉联接
---------------------------------交叉联接得到的是两表联接所有的数据组合
------------------------------------------(A表的数据记录*
B
表的数据记录)
---------------------方式一
select
Student.*,score.*
from
Student,score
------------------------方式二
select
score
.*,Student.*
from
Student
cross
join
score
-------------------------------多表联接
-----------要求查出张三
C#的考试成绩,涉及student,score,subject三个表
---------方式一:
select
student.name,subject.sname
,score
.score
from
Student
inner
join
score
on
student.id=
score.id
inner
join
subject
on
score.id=subject.id
where
Student.name='张三'
and
subject.sname='C#'
---------方式二:等值联接
select
student.name,subject.sname
,score
.score
from
Student,score
,subject
where
StudentDB.id=score.id
and
score
.id=subject.id
and
Student.name='张三'
and
subject.sname='C#'
Ⅶ Sql多表查询,怎么做
根据你的查询结果要求,可以根据一下几个步骤确定多表查询语句的写法:
1、要显示所有学生信息、班级、年级等信息,则需以主表1为主记录,其他表通过外连接的方式进行关联;
2、LEFT JOIN 关键字会从左表那里返回所有的行,即使在右表中没有匹配的行,确定主表之后,其他关联表使用LEFT JOIN;
3、拼接SQL语句,需要确定关联字段主表1与表2的关联为主表1.studentid=表2.studentid,
主表1与表3的关联为主表1.gradId=表3.gradId,主表1与表4的关联为主表1.classId=表4.classId
4、具体语句为:
SELECT 表1.ID, 表2.STUDENTNAME,表3.GRADNAME,表4.CLASSNAME
FROM 表1
LEFT JOIN 表2 ON 表1.STUDENTID = 表2.STUDENTID
LEFT JOIN 表3 ON 表1.GRADID = 表3.GRADID
LEFT JOIN 表4 ON 表1.CLASSID= 表4.CLASSID
Ⅷ 简单的SQL多表查询语句!
select
*
from
table_A
ta,tale_B
tb
where
ta.id
=
tb.id
或者
select
*
from
table_A
ta
inner
join
tale_B
tb
on
ta.id
=
tb.id
(其中,两张表关联的话,至少有一个ta.id
=
tb.id,三张表关联查询的话,至少有两个关系=,以此类推...)
Ⅸ sql多表联查实例
sql多表联查实例
下面提供四款sql多表关联查询的实例,个个效率不一样。
select
*
from
order_info
as
a
,ivrlog4ivrdlvinst
as
b
where
(a.saleorder=b.ext1_skill
and
b.start_date=@date1
and
se_id='55'
and
b.ext1_skill!='')
and
convert(varchar(10),a.instime,112)=@date2
and
max(a.instime)
方法二
select
*
from
order_info
as
a
where
a.saleorder=(
select
b.ext1_skill
from
ivrlog4ivrdlvinst
as
b
where
b.start_date=@date1
and
se_id='55'
and
b.ext1_skill!='')
and
convert(varchar(10),max(a.instime),112)=@date2
方法三
declare
@date1
varchar(20),
@date2
varchar(20)
set
@date1='20100812'
set
@date2='2010-08-12'
select
*
from
order_info
as
a
where
a.saleorder=
(select
b.ext1_skill
from
ivrlog4ivrdlvinst
as
b
where
b.start_date=@date1
and
se_id='55'
and
b.ext1_skill!='')
and
convert(varchar(10),a.instime,112)=@date2
and
max(a.instime)
方法四
select
b.caller,
b.start_date,
b.start_time,
b.ext1_skill,
c.deliveryno,
c.destroyresult,
c.deliverydate,
c.deliverytime,
c.arrangetime,
c.driverphone,
c.drivermobile,
a.servicedate,
a.servicetime,
a.workertel
from
order_info
as
a
,ivrlog4ivrdlvinst
as
b
,delivery_info
as
c
where
a.saleorder
in
(select
b.ext1_skill
from
ivrlog4ivrdlvinst
where
b.start_date=@date1
and
b.se_id='55'
and
b.ext1_skill!='')
and
convert(varchar(10),a.instime,112)=@date2
order
by
b.start_date
desc,
b.start_time
desc
Ⅹ SQL多表查询语句怎么写
SQL多表查询语句的步骤如下:
我们需要准备的材料分别是:电脑、sql查询器。
1、首先,打开sql查询器,连接上相应的数据库表,例如m1表和m2表。