exists在postgresql怎么用? 谢谢了
发布网友
发布时间:2022-04-23 20:41
我来回答
共1个回答
热心网友
时间:2023-07-15 14:48
例学生表(t_student)和班级表(t_class)
班级表:
create table t_class (
id SERIAL not null,
name VARCHAR(20) null,
constraint PK_T_CLASS primary key (id)
);
学生表:
create table t_student (
id SERIAL not null,
name VARCHAR(20) null,
class_id INT8 null,
constraint PK_T_STUDENT primary key (id)
);
alter table t_student
add constraint FK_T_STUDEN_REFERENCE_T_CLASS foreign key (class_id)
references t_class (id)
on delete restrict on update restrict;
select * from t_student t1
where exists (select * from t_class t2 where t2.id = t1.class_id and t2.name = '一年级一班' );追问谢谢,那你看看我这段SQL的问题:
while( exists (select wl_bh from sbwl_xx group by wl_bh having count(*)>=2 limit 1))
Begin
delete from sbwl_xx where wl_bh in(select wl_bh from sbwl_xx group by wl_bh having count(*)>=2 limit 1)
End
追答我觉得是exists (select wl_bh from sbwl_xx group by wl_bh having count(*)>=2 limit 1)会被看做是一个loop;
我的建议是:
declare
r record;
wlbh "varchar";//根据你 wl_bh的类型来定
sql "varchar";
begin
sql :='select wl_bh from sbwl_xx group by wl_bh having count(*)>=2 limit 1';
for r in execute sql loop
wlbh :=r.wl_bh;
delete from sbwl_xx where wl_bh=wlbh;
end loop;
end;
不知是否对你有帮助。