sqlserver联合查询
㈠ sqlserver多表联合查询
selectc.discount
froma,b,cwherea.hw_name=b.hw_name
andb.sort_id=c.sort_id
anda.hw_name='苹果'
㈡ 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 同时查询多表数据
在sqlserver中新建个视图,很直观看,相查哪个数据,sql语句直接就出来了。
㈣ SQLServer两张表联合查询(一对多)...
select t1.U_Id,t1.U_Name,sum(isnull(t2.G_Price,0)) as G_Price
from Users t1 left join Goods t2
on t1.U_Id=t2.G_UId
group by t1.U_Id,t1.U_Name
order by t1.U_Id
㈤ SQLserver 联合查询排序问题
不知道你原来的sql语句啥样子啊。。。
SELECT
ISNULL(sale_item, '总计') AS item,
CASE
WHEN GROUPING(sale_item) = 1
AND GROUPING(STR(DATEPART(qq, sale_date))) = 1
THEN '总计'
WHEN GROUPING(sale_item) = 0
AND GROUPING(STR(DATEPART(qq, sale_date))) = 1
THEN '小计'
ELSE
STR(DATEPART(qq, sale_date))
END AS sale_q,
SUM(sale_money) [money]
FROM
sale_report
GROUP BY
sale_item, STR(DATEPART(qq, sale_date)) WITH ROLLUP;
item sale_q money
---- ---------- ----------------------------------------
A 1 180810.00
A 2 182819.00
A 3 184828.00
A 4 184828.00
A 小计 733285.00
B 1 180.00
B 2 455.00
B 3 735.00
B 4 1012.00
B 小计 2382.00
C 1 1398.00
C 2 1426.00
C 3 1457.00
C 4 1457.00
C 小计 5738.00
T 3 100.00
T 小计 100.00
总 总计 741505.00
(18 行受影响)
<hr/>
如果希望 总计、小计显示在上面, 那么 ORDER BY 设置一下.
SELECT
ISNULL(sale_item, '总计') AS item,
CASE
WHEN GROUPING(sale_item) = 1
AND GROUPING(STR(DATEPART(qq, sale_date))) = 1
THEN '总计'
WHEN GROUPING(sale_item) = 0
AND GROUPING(STR(DATEPART(qq, sale_date))) = 1
THEN '小计'
ELSE
STR(DATEPART(qq, sale_date))
END AS sale_q,
SUM(sale_money) [money]
FROM
sale_report
GROUP BY
sale_item, STR(DATEPART(qq, sale_date)) WITH ROLLUP
ORDER BY
GROUPING(sale_item) desc, item,
GROUPING(STR(DATEPART(qq, sale_date))) desc
item sale_q money
---- ---------- ----------------------------------------
总 总计 741505.00
A 小计 733285.00
A 1 180810.00
A 2 182819.00
A 3 184828.00
A 4 184828.00
B 小计 2382.00
B 1 180.00
B 2 455.00
B 3 735.00
B 4 1012.00
C 小计 5738.00
C 1 1398.00
C 2 1426.00
C 3 1457.00
C 4 1457.00
T 小计 100.00
T 3 100.00
(18 行受影响)
㈥ sqlserver多表查询
既然三个表都是有关联的,先单表统计
,再合起来统计。。
select
A.*,BC.sum_B,BC.sum_C
from
A表
as
A
join
(
select
单位编码,sum_B,sum_C
from
(select
单位编码,sum(数据B)
as
sum_B
from
B表
group
by
单位编码)
as
B
join
(select
单位编码,sum(数据C)
as
sum_C
from
C表
group
by
单位编码)
as
C
on
B.单位编码=C.单位编码
)
as
BC
on
A.单位编码=BC.单位编码
简化下:
select
A.*,B.sum_B,C.sum_C
from
A表
as
A
join
(select
单位编码,sum(数据B)
as
sum_B
from
B表
group
by
单位编码)
as
B
on
A.单位编码=B.单位编码
join
(select
单位编码,sum(数据C)
as
sum_C
from
C表
group
by
单位编码)
as
C
on
A.单位编码=C.单位编码
㈦ sqlserver 两表联合查询,且查询指定日期下,所有类别的数据,无数据为Null
select B.MonthID,B.[Month],B.[Order],A.[Date],sum(isnull(A.Price,0))as Price from pp2 A left join pp B
on A.MonthID=B.MonthID
where B.[Order]<=5
group by B.MonthID,B.[Month],B.[Order],A.[Date]
order by B.MonthID
把表名换掉就行了。
㈧ “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