① “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
② sqlserver 触发器 多表联查
CREATE TRIGGER [dbo].[表1_UPDATE_表2] --这里是触发器名称,最好一目了然的
ON [dbo].[表1]
FOR UPDATE,INSERT,DELETE --更新,插入,删除时触发(根据实际情况,可以只用一个)
AS
BEGIN
--下面是你的语句,我没有修改,只是调整了排版和大小写
UPDATE 表2
SET name=0,age=''
FROM 表2 INNER JOIN
(
SELECT DISTINCT ABS(id) AS billid,sale_z.ele_sordercode
FROM 表3
WHERE ele_sordercode<>''
) 表3
ON 表2.tid=表3.id LEFT JOIN
(
SELECT MAX(id) AS id, referbillid
FROM 表1
WHERE referbillid<>0
GROUP BY referbillid
) 表1 ON 表3.id=表1.id
WHERE name=1 AND refer.id IS NULL
END
③ sqlserver多表联合查询
select aa.a_name as 名字,count(aa.a_name) as 数量 from a aa,b bb where aa.a_id = bb.a_id group by aa.a_name
④ sqlserver里如何实现联合主键及如下自增功能
不会,不过有些思路
提取你要输入的A的值,判断 select count(*) from TABLE where A=你输入的值。
case1:如果查到的值是0,则B为1;
case2:如果查到的值不是0,查询 select max(B)+1 from TABLE where A=你输入的值 ,并将查询到的值和A要输入的值一起插入;
如果只是用数据库实现的话带参数的存储结构应该能实现你这个要求,编程的话就更简单了
⑤ sqlServer 2005 中内连接(inner join)与左连接(left join)的区别最好附加个实例
inner join 是内连接,left join 是左连接。
举例:
a表,有数据 id1(1,2,3,4,5)
b表,有数据 id2(1,3,5,6,7)
select a.* from a inner join b on a.id1 = b.id2 结果,是id1(1,3,5)
select a.* from a left join b on a.id1 = b.id2 结果,是 id1(1,2,3,4,5)
⑥ 请教大神SQlSERVER向这样多表联合查询怎么写语句
SELECT s.*,p.player_name FROM t_match_score s
LEFT JOIN t_match_player p
ON s.player_id=p.player_id
⑦ sqlserver 内联查询问题
应该要letf join两次。
下面的SQL是 MYSQL的写法,没在MSSQL测试过,但逻辑是一样的。
两表:
teams(id,t1_id,t2_id)
team(id,name)
selectt1_id,t1.name,t2_id,t2.name
fromteamsasts
leftjointeamast1onts.t1_id=t1.id
leftjointeamast2onts.t2_id=t2.id
⑧ sqlserver中的关联查询问题
sql server 本省对语句就有自动优化功能, 第一个里边where语句相当于join on 来操作的
这样看来两个的效率基本上是一样的,你可以做两个表试一试。但是join的写法有助于你的编写语法检查,和易读性
⑨ sqlserver 创建视图失败,原因:ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效
sqlserver中创建视图时,as下不能出现order by
其实你要排序的话可以对视图进行排序嘛,没必要在视图创建时候就排序
--创建视图
create view v$_emp_1
as
select * from emp
where emp.sal > (select min(sal) from emp)
--查询视图
select * from v$_emp_1 order by sal--这儿排序不就行了嘛~~~采纳哦