表1
UID P_ID name
1 1 3
2 1 5
2 2 5
取出的结果为
UID P_ID name
1 1 3
2 2 5
我自己写了个sql语句,但是查询速度很慢,要一分多钟.
sql 表中有一个字段UID有重复值,把不重复UID的取出来,重复的取其中P_ID=2的一条
答案:2 悬赏:70 手机版
解决时间 2021-01-27 01:20
- 提问者网友:最美的风景
- 2021-01-26 09:38
最佳答案
- 五星知识达人网友:北方的南先生
- 2021-01-26 10:25
create table a
(
u_id int,
p_id int,
name int
)
insert into a (u_id,p_id,name ) values (1,1,3);
insert into a (u_id,p_id,name ) values (2,1,5);
insert into a (u_id,p_id,name ) values (2,2,6);
insert into a (u_id,p_id,name ) values (3,1,7);
insert into a (u_id,p_id,name ) values (3,2,8);
insert into a (u_id,p_id,name ) values (3,3,9);
insert into a (u_id,p_id,name ) values (3,4,0);
select a.* from a inner join (select u_id,MAX(p_id) as x from a where p_id<=2 group by u_id) b
on a.u_id=b.u_id and a.p_id =b.x
order by a.u_id
(
u_id int,
p_id int,
name int
)
insert into a (u_id,p_id,name ) values (1,1,3);
insert into a (u_id,p_id,name ) values (2,1,5);
insert into a (u_id,p_id,name ) values (2,2,6);
insert into a (u_id,p_id,name ) values (3,1,7);
insert into a (u_id,p_id,name ) values (3,2,8);
insert into a (u_id,p_id,name ) values (3,3,9);
insert into a (u_id,p_id,name ) values (3,4,0);
select a.* from a inner join (select u_id,MAX(p_id) as x from a where p_id<=2 group by u_id) b
on a.u_id=b.u_id and a.p_id =b.x
order by a.u_id
全部回答
- 1楼网友:妄饮晩冬酒
- 2021-01-26 12:01
1. select 学号 from table1 where 学号 in ( select 学号 from table2) and 学号 in (select 学号 from table3)
2. select 学号 from table2 where 学号 in ( select 学号 from table3)
3. select 学号 from table1 where 学号 in ( select 学号 from table3)
4. select 学号 from table3 where 学号 not in ( select 学号 from table1) and 学号 not in ( select 学号 from table2)
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯