永发信息网

sql 表中有一个字段UID有重复值,把不重复UID的取出来,重复的取其中P_ID=2的一条

答案:2  悬赏:70  手机版
解决时间 2021-01-27 01:20
表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语句,但是查询速度很慢,要一分多钟.
最佳答案
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
全部回答
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)
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
摩托驾驶证和汽车驾驶证合到一起后能注销一个
龙涛汽车维修地址在什么地方,我要处理点事
兀突的意思是什么啊?知道的请说下!
映红楼茶楼地址在什么地方,想过去办事
Usethebin的意思是什么?
吃绿豆解暑好,还是西瓜解暑好。(请完整回答)
翠眊的意思是什么啊?知道的请说下!
胆朗息肉,内有个大小的0.3x0.3cm的稍强回声附
小华在课堂学习中,整理了如下内容:①“普法
乐腾汽车俱乐部(车大夫汽车养护中心)地址在什
宴春茶楼地址在什么地方,想过去办事
大闸蟹洗了放冰箱明天蒸可以?
电脑控制多个音响
金项链可以去别的地方换款式吗
车动力全自动电脑洗车美容装具地址在什么地方
推荐资讯
反季节蔬菜价格曾一度上升,南方这些蔬菜的种
英特尔奔腾g3260和英特尔i3哪个好
三菱PLC 指令中MOVP HOFF K2Y0什么意思
日主天干土生于春季,喜有金,意思是?
to take this adventure course will certain
嘉珍的意思是什么啊?知道的请说下!
东联网吧地址在什么地方,想过去办事
纵舍的意思是什么啊?知道的请说下!
2017年油性胶水的执行标准
单选题48和60的最大公约数,去除它们的最小公
英勋的意思是什么啊?知道的请说下!
驩心的意思是什么啊?知道的请说下!
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?