sql查多行
㈠ sql语句 查询所有行
select 种类字段,count(*) from 表 group by 种类字段
㈡ 怎么用sql语句同时查询多条数据
这个描述不够清晰啊,查询多条数据的意思是什么,返回结果中包含多行数据?SQL语句返回的结果正常情况下返回的都是一张临时表,里面可以包含多行多列。
如果不是你想问的情况,麻烦把问题进一步描述得更清楚一些。
㈢ sql语句 多行函数查询
想法1
先查询出以每个部门为单位,有多少人工资低于3000 以及 低于3000的人数 顺便把部门ID也带出来,以备后用
select sum(salary),count(id),dep_id from yuangong where salary<3000 group by dep_id
然后,将上面查询出来的结果进行加工,得出最后结论
select 
 bumen.dep_name,
 a.renshu,
 a.money/a.renshu
from
 bumen,
 (select sum(salary) as money,count(id) as renshu,dep_id from yuangong where salary<3000 group by dep_id) a
where 
 a.dep_id = bumen.id
 
想法呢是这个想法,
想法2
先把所有的部门,工资少许8000 的数据找出来
select 
 bumen.dep_name,
 yuangong.*
from
 bumen,
 yuangong
where 
 yuangong.salary<3000 and
 yuangong.dep_id = bumen.id
 
然后对上面的数据进行分类加工,
分类的依据是部门id 或者部门名字(group by bumen.dep_name)
select 
  bumen.dep_name,
  count(yuangong.id),
  sum(yuangong.salary)/count(yuangong.id)
from
 bumen,
 yuangong
where 
 yuangong.salary<3000 and
 yuangong.dep_id = bumen.id
group by bumen.dep_name
㈣ sql怎么同时写多行一样的查询语句
sql怎么同时写多行一样的查询语句
最简捷直观的方法就是利用分组
1
2
3
4
5
6
select ID,
       Sum(语文) 语文,
       Sum(数学) 数学,
       Sum(英语) 英语   
  from scores
 group by ID
 你可以直接用下面的语句在SQL Server中测试结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
DECLARE @ScoresVar table(
    ID int NOT NULL,
    Chinese int,
    Math int,
    English int);
     
Insert into @ScoresVar
values(1 ,70, null, null)
Insert into @ScoresVar
values(1 ,null,80, null)
Insert into @ScoresVar
values(1 ,null, null, 90)
Insert into @ScoresVar
values(2 ,75, null, null)
Insert into @ScoresVar
values(2 ,null,85, null)
Insert into @ScoresVar
values(2 ,null, null, 95)
select ID, 
       SUM(Chinese) Chinese, 
       Sum(Math) Math,
       Sum(English) English
  from @ScoresVar 
 group by ID
 还有一种方法,可能更通用一点,利用 FOR XML PATH 和STUFF函数来做,虽然复杂,但是对表的数据类型无限制。
还是上面的测试数据,你可以通过下面的代码来得到你想要的数据:
1
2
3
4
5
6
7
8
9
SELECT ID,
   STUFF((SELECT ',' + Cast(A.Chinese as varchar)
        FROM @ScoresVar A Where A.ID=B.ID FOR XML PATH('')),1,1,'') As Chinese,
    STUFF((SELECT ',' + Cast(A.Math as varchar)
        FROM @ScoresVar A Where A.ID=B.ID FOR XML PATH('')),1,1,'') As Math,
    STUFF((SELECT ',' + Cast(A.English as varchar)
        FROM @ScoresVar A Where A.ID=B.ID FOR XML PATH('')),1,1,'') As English  
  From @ScoresVar B
  Group By ID
1
2
3
4
-- 输出结果 
ID Chinese Math English
1     70     80     90
2     75     85     95
㈤ sql中查询获取多个行数的语句表达式怎么写
参看语句:
现用ROW_NUMBER()将所有结果按照你想要的顺序排序生成需号
然后根据你的要求取
SELECT*FROM(
SELECT*,ROW_NUMBER()OVER(ORDERBYPuReiptID)asSeqNoFROMPuReceipt
)AStWHEREt.SeqNoBETWEEN500AND600
