现在有表A,详细如下
id name job
1 路飞 海贼
2 红发 海贼
3 库赞 海军
4 蒙卡 海军
5 smoke 海军
我想把这个表遍历出来的结果如下
job name
海贼 路飞
红发
海军 库赞
蒙卡
smoke
******重点是job这列,不是每行都显示,要动态的rowspan 每个job的个数.求高手指点,有语句更好,谢谢!
sql查询语句请帮帮忙,谢谢!
答案:3 悬赏:10 手机版
解决时间 2021-02-28 20:00
- 提问者网友:wodetian
- 2021-02-28 14:55
最佳答案
- 五星知识达人网友:罪歌
- 2021-02-28 15:50
$ sqlite3
SQLite version 3.7.7 2011-06-23 19:49:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .mode column
sqlite> create table tb_tester (
...> id int,
...> name varchar(16),
...> job varchar(16)
...> );
sqlite>
sqlite> insert into tb_tester values (1,'路飞','海贼');
sqlite> insert into tb_tester values (2,'红发','海贼');
sqlite> insert into tb_tester values (3,'库赞','海军');
sqlite> insert into tb_tester values (4,'蒙卡','海军');
sqlite> insert into tb_tester values (5,'smoke','海军');
sqlite>
sqlite> select
...> case when not exists (
...> select 1
...> from tb_tester b
...> where b.job=a.job
...> and b.id < a.id) then job else '' end as job
...> , name
...> from tb_tester a
...> order by a.job desc, id;
海贼 路飞
红发
海军 库赞
蒙卡
smoke
sqlite>
SQLite version 3.7.7 2011-06-23 19:49:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .mode column
sqlite> create table tb_tester (
...> id int,
...> name varchar(16),
...> job varchar(16)
...> );
sqlite>
sqlite> insert into tb_tester values (1,'路飞','海贼');
sqlite> insert into tb_tester values (2,'红发','海贼');
sqlite> insert into tb_tester values (3,'库赞','海军');
sqlite> insert into tb_tester values (4,'蒙卡','海军');
sqlite> insert into tb_tester values (5,'smoke','海军');
sqlite>
sqlite> select
...> case when not exists (
...> select 1
...> from tb_tester b
...> where b.job=a.job
...> and b.id < a.id) then job else '' end as job
...> , name
...> from tb_tester a
...> order by a.job desc, id;
海贼 路飞
红发
海军 库赞
蒙卡
smoke
sqlite>
全部回答
- 1楼网友:独行浪子会拥风
- 2021-02-28 18:28
1insert into teachers values('300008',' 杨梦',' 女 ', '59', ' 66/04/22','yes','1660' ,'210' )2 delete teachers where 年龄<36 and 性格='女'3 update teachers set 应发工资=应发工资+应发工资*1/5 where (year(getdate())-year(参加工作年月))>254.select 教师号,姓名,应发工资-扣除工资 as 实发工资 from teachers5.select count(*) as 教师的人数,avg(应发工资-扣除工资) from teachers6.select 教师号,姓名,应发工资-扣除工资 as 实发工资 from teachers where year(参加工作年月)<19917.select max(应发工资-扣除工资),min(应发工资-扣除工资),avg(应发工资-扣除工资) from teachers8.select 教师号,姓名 from teachers where 党员='yes' order by 年龄 desc 1.select count(学号) from students group by 教师号2.select min(成绩) as 最低分,max(成绩) as 最高分 ,avg(成绩) as 平均成绩
from students group by 教师号3.select 教师号,姓名,性别 from students where 学号=030012
- 2楼网友:雾月
- 2021-02-28 17:15
select (case when rn = 1 then job else '' end) as job, name from
(select job, name,
ROW_NUMBER() over(partition by job order by id) as rn from A) as temp
order by job这是限于你按id排序就已经按job分类好的情况
如果是
id name job
1 路飞 海贼
2 库赞 海军
3 蒙卡 海军
4 红发 海贼
5 smoke 海军
这样的话需要改一下写法
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯