如有3个表格: 
table1: id1,id2,msg 
table2: id2, id3 
table3: id3, text 关联索引:table1.id2 = table2.id2, table2.id3 = table3.id3 我要搜索table1表中记录,条件是table3.text='aa',用一条sql语句可以吗?多谢? 我尝试了 
select table1.* from table1 inner join table2 ,table3 on table1.id2=table2.id2 and table2.id3=table3.id3 where table3.text='aa'
但是不行

解决方案 »

  1.   

    select table1.*
    from table1 inner join table2  on table1.id2 = table2.id2
    inner join table3 on table2.id3 = table3.id3 
      

  2.   

    1 楼忘加where table3.text='aa' 了select table1.*
    from (table1 inner join table2  on table1.id2 = table2.id2)
    inner join table3 on table2.id3 = table3.id3 
    where  table3.text='aa'

    或者select table1.*
    from table1,table2,table3
    where table1.id2=table2.id2 and table2.id3=table3.id3 
    and table3.text='aa'
    http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#join
    13.2.7.1. JOIN语法
    MySQL支持以下JOIN语法。这些语法用于SELECT语句的table_references部分和多表DELETE和UPDATE语句:
    
      

  3.   

    select 
      table1.* 
    from 
      table1 
    inner join 
      table2 
    on 
      table1.id2=table2.id2 
    inner join 
      table3 
    on
     table2.id3=table3.id3 
    where 
      table3.text='aa'--或者select
      table1.*
    from
      table1,
      table2,
      table3
    where
      table1.id2=table2.id2
    and
      table2.id3=table3.id3
    and
      table3.text='aa'