我有一个主表,一个从表,从表中的其中一个字段是主表的ID字段。
如table1中有id字段(不重复的),然后table2中有table1_id字段,table1_id对应表1中的id字段(可能会有多个),我想查询表2中的关键字要求的显示出表1中的记录.但是我用select a.* from table1 a left join table2 b on a.id = b.table1_id where b.name  like 'keyword'时,会出现重复的记录(当table1_id有多条相同记录时),但是我只想取出符合keyword记录的表1的不重复记录.

解决方案 »

  1.   

    http://topic.csdn.net/u/20100204/12/95a42407-db8d-4a60-bf95-e35a136307d0.html
      

  2.   


    select distinct a.* from table1 as a 
    left join table2 as b 
    on a.id = b.table1_id 
    where b.name like 'keyword'试试?
      

  3.   

    select a.* from table1 a where exists(select name from table2 b where a.id = b.table1_id and name like 'keyword')
      

  4.   

    select a.* from table1 a left join table2 b on a.id = b.table1_id where b.name like 'keyword' GROUP BY a.aa,a.bb --a表中的字段名