‘壹’ sql语句查询数据是和三张表有关联的应该怎么写
select
a.*,b.*,c.*
from
a
inner
join
b
on
a.a1
=
b.b1
left
join
c
on
a.a2
=
c.c2
where
a.a1='xx'
主要用标准sql的inner
join
,left
join
,right
join
进行连接,左连,右连;
on
后面可以加括号,加其他条件,也可以用括号来确定先连接哪个再连接哪个
‘贰’ 求三表联合查询的SQL查询语句
车讯语句:select username,psw from (a1 left join a2 on a1.a1_id=a2.a1_id) left join a3 on a1.a1_id=a3.a1_id
这样写:
SELECT
S.SName AS 姓名, CS.CourseName AS 课程, C.Score AS 成绩
FROM Students AS S
INNER JOIN Score AS C ON (S.SCode = C.StudentID)
INNER JOIN Course AS CS ON (CS.CourseID = C.CourseID
(2)sql三表语句扩展阅读:
SQL联合查询的分类
一、内连接查询:只查询左边表有且右边表也有的数据,本质上是依据外键关系,在笛卡尔积查询的基础上过滤出正确的数据。
语句有2种形式:
Select * from dept ,emp where dept.id=emp.dept_id
Select * from dept inner join emp on dept.id =emp.dept_id
二、外连接查询:外连接是用于查询俩边一边有一边没有的数据。
三、左外连接查询:在内连接的基础上增加上左边表有而右边表没有的数据
语句:Select * from dept join emp on dept.id=emp.dept_id
四、右外连接:在内连接的基础上增加上右边表没有的记录
语句:Select * from dept right join emp on dept.id =emp.dept_id
‘叁’ sqlserver三表联查sql语句
假设学生表叫student,课程表叫class,选课表叫choose
1.三层嵌套的问题
select student.name from student where student.id IN
(select choose.sid from choose where choose.cid NOT IN
(select class.id from class where class.teacher='李明'))
2.一个内连接,一个嵌套
select student.name,avg(choose.score) from
student inner join choose on student.id=choose.sid
where student.id IN
(select choose.sid from choose
where choose.score<'60'
group by choose.sid
having count(choose.sid)>=2)
gruop by student.id
3.一个联合查询,一个嵌套查询
select student.name from student
where student.id IN
(select c1.sid from choose c1 where choose.cid='1'
union
select c2.sid from choose c2 where choose.cid='2'
on c1.sid=c2.sid
)
4.其实就是自连接查询和行列交换的问题:
select student.id,
(case choose.id when '1' then choose.score end) as 1号课成绩,
(case choose.id when '2' then choose.score end) as 2号课成绩,
from student inner join choose on student.id=choose.sid sc1,
student inner join choose on student.id=choose.sid sc2
where sc1.id='1'
and sc2.id='2'
and sc1.score>sc2.score
‘肆’ SQL三表联合查询的语句如何优化
selectt_cp.id,t_cp.proc,t_odid_cpid.num
fromt_odid_cpid
leftjoint_cpont_cp.id=t_odid_cpid.cpid
leftjoinT_ORDERont_odid_cpid.odid=T_ORDER.odid
WhereT_ORDER.B_zzdm='785390650'。
‘伍’ 如何sql三张表关联查询
三张表关联查询
使用场景:A\B\C 三张表,现在要查询并展示A表和C表中的某些字段,但是A、C两表没有相同字段,无法关联,此时有B表恰好有两个字段,一个字段和A表一个字段相同,一个字段和C表一个字段相同,我们称B表为“中间表”,因此通过B表把A、C表关联起来
SELECT A1,A2,C1,C2 --展示A表中的A1\A2字段和C表中的C1\C2
FROM B --中间表
INNER JOIN A ON A.A1 = B.B1 --A表中的与B表中相同的字段
INNER JOIN C ON C.C1 = B.B1 --C表中的与B表中相同的字段
where xxxxx ---条件你自己按照需求来加,没有条件就不写where了
‘陆’ 写出创建如下三张数据表的SQL语句。
在SSMS中,用【创建表】来设计,包括约束;然后保存,选中该新表,右键【编写表脚本】,SQL就有了
项目”表结构:项目号:普通编码定长字符类型,长度为10,主键。项目写出创建如下三张表的sQL语句。
按照要求用SQL语句创建表格:选课表:表名——SC属性:Sno字符型,最大7个字符表示学生学号。
Cno字符型,最大4个字符表示课程号。Grade整型表示成绩。约束:主码——Sno,Cno;Sno,Cno均为外码)
‘柒’ 求三表联合查询的SQL查询语句
第一种方法:select * from student,teacher,project wherestudent.id=teacher.sid andstudent.id=project.sid;
第二种:select * from student inner join teacher onstudent.id=teacher.sid inner join project onstudent.id=project.sid。
‘捌’ 三表联查的SQL语句
这问题交给我吧,假设学生表叫student,课程表叫class,选课表叫choose
1.三层嵌套的问题
select student.name from student where student.id IN
(select choose.sid from choose where choose.cid NOT IN
(select class.id from class where class.teacher='李明'))
2.一个内连接,一个嵌套
select student.name,avg(choose.score) from
student inner join choose on student.id=choose.sid
where student.id IN
(select choose.sid from choose
where choose.score<'60'
group by choose.sid
having count(choose.sid)>=2)
gruop by student.id
3.一个联合查询,一个嵌套查询
select student.name from student
where student.id IN
(select c1.sid from choose c1 where choose.cid='1'
union
select c2.sid from choose c2 where choose.cid='2'
on c1.sid=c2.sid
)
4.好吧,看起来很难,其实就是自连接查询和行列交换的问题
select student.id,
(case choose.id when '1' then choose.score end) as 1号课成绩,
(case choose.id when '2' then choose.score end) as 2号课成绩,
from student inner join choose on student.id=choose.sid sc1,
student inner join choose on student.id=choose.sid sc2
where sc1.id='1'
and sc2.id='2'
and sc1.score>sc2.score
5.至于你说的insert报错的问题,我想可能是因为学生ID和课程ID这两个外键有重复的值,
你可以检查下,实在不行删除外键,插入数据,在这里外键对你最后要的结果影响不大。
纯手工打造~有帮助记得给分哈