使用like query在两个表中获取所需的输出

时间:2011-08-08 09:56:11

标签: mysql hibernate

大家好我正在尝试实现以下查询,根据给定的条件从两个表中返回结果。如何将正确的查询放到预期的输出中?

SELECT * FROM  bw_tempclientdetails  
where companyname like '%fff%'  
not in (SELECT * FROM bw_clientallocation where companyname like '%fff%');

3 个答案:

答案 0 :(得分:2)

SELECT * FROM  bw_tempclientdetails  
where companyname like '%fff%' and companyname   
not in (SELECT companyname FROM bw_clientallocation where companyname like '%fff%');

答案 1 :(得分:1)

使用join< - 点击链接

SELECT * 
  FROM  bw_tempclientdetails bw_temp
  LEFT JOIN bw_clientallocation bw_client
    ON bw_temp.companyname = bw_client.companyname  -- this is just an identifier or link between the tables
 WHERE bw_client.company LIKE '%fff%'
   AND (bw_temp.companyname LIKE '%fff%' AND bw_client.company LIKE '%fff%');

希望它有所帮助。祝你好运。

答案 2 :(得分:0)

select t1.* from (
SELECT * FROM  bw_tempclientdetails  
where companyname like '%fff%' ) as t1
left join (SELECT * FROM bw_clientallocation where companyname like '%fff%') as t2
on t1.companyname = t2.companyname
where t2.companyname is null