mysql - query three tables

夙愿已清 提交于 2019-12-02 01:28:27

Mysql Join

Try this

select * from table1 t1
join table2 t2 on t1.t2ref = t2.id
join table3 t3 on t2.t3ref = t3.id

Add a where clause to search for certain rows in table1

where t1.field = 'value'
Rik Heywood

yes

SELECT t3.* 
  FROM t1, t2, t3
  WHERE t1.id = t2.id
    AND t2.otherid = t3.id
    AND t1.id = XXXX

you want to use a join:

   SELECT `t3`.`id`
     FROM `table3` `t3`
LEFT JOIN `table2` `t2`
       ON `t3`.`foreign_id` = `t2`.`id`
LEFT JOIN `table1` `t1`
       ON `t2`.`foreign_id` = `t1`.`id`
    WHERE `t1`.`id` = 'some_id'

Use the JOIN command to link your tables to oneantother.

Additionally I'd recommend this tutorial by Keith Brown.

You want to do a join. There is a lot of tutorials about this and various ways of doing it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!