两个表结构如下:
table1(id,userid) 无主键(有可能1个id对应多个userid)
table2(id,acountid,fileid) id为主键table1中的id来源与table2中的id求查询 满足userid= 条件1 或 acountid= 条件2 的所有fileid 的sql语句
table1(id,userid) 无主键(有可能1个id对应多个userid)
table2(id,acountid,fileid) id为主键table1中的id来源与table2中的id求查询 满足userid= 条件1 或 acountid= 条件2 的所有fileid 的sql语句
*
FROM
TABLE1 A
JOIN
TABLE2 B ON A.ID=B.ID
WHERE
A.USERID=? OR B.ACOUNTID=??
這樣?
from table1 a,table2 b
where a.id=b.id and (a.userid=条件1 or b.acountid=条件2)
select n.fileid from table1 m , table2 n where m.id = n.id and userid= 条件1 and acountid= 条件2
WHERE userid ='条件' or acoutid ='条件'
如果table2的某条记录满足acountid= 条件2,但该记录的id没有在table1中出现,
按上面的where table1.id = table2.id 条件就会漏掉该条记录。
select distinct table2.id from table2 right join table1 on table1.id = table2.id where table1.userid=条件1 or table2.acountid=条件2)
WHERE
A.USERID=xx OR B.ACOUNTID=xx
select distinct table2.id from table2 right join table1 on table1.id = table2.id where table1.userid=条件1 or table2.acountid=条件2)
select distinct table2.id from table1 right join table2 on table1.id = table2.id where table1.userid=条件1 or table2.acountid=条件2)
select table2.fiele
from table1,table2
where table1.id=table2.id and (userid= 条件1 or acountid= 条件2)
FROM TABLE2 A LEFT JOIN TABLE1 B ON (A.ID=B.ID)
WHERE A.USERID=?? OR A.ACOUNTID=??
select distinct fieldid
from table2 t2
where accountid = con2
or exists
(select id
from table1 t1
where userid = con1
and t1.id = t2.id)
con1=条件1
con2=条件2
在表比较大的时候这个效率比join要高
select distinct table2.id from table1 right join table2 on table1.id = table2.id where table1.userid=condition1 or table2.acountid=condition2)