高分求两个表的SQL查询语句,两个表没有外键!

2025-03-01 21:05:13
推荐回答(2个)
回答1:

现已知 table a中的flid字段的一个值,如flid=1;
根据此值先筛选此表中flid值为1所有对应的id(但只取唯一一个id的值),
sql:
select distinct id from table a where flid=1;
再根据取出来的table a中id值判断与table b 表的class字段的某个值是否相等,
select * from table b where class = (select distinct id from table a where flid=1)
要筛选table b 中class相同的所有id字段的集合
select sum(id) from table b where class = (select distinct id from table a where flid=1)

回答2:

select b.* from a,b where a.id=b.class and a.flid=1

是这个意思吗?