mysql - query three tables

后端 未结 5 1431
庸人自扰
庸人自扰 2021-01-22 19:06

I have a relational database with three tables. The first containts id\'s that relate to the second. The second contains id\'s that relate to the third. The third contains the r

相关标签:
5条回答
  • 2021-01-22 19:39

    yes

    SELECT t3.* 
      FROM t1, t2, t3
      WHERE t1.id = t2.id
        AND t2.otherid = t3.id
        AND t1.id = XXXX
    
    0 讨论(0)
  • 2021-01-22 19:41

    Use the JOIN command to link your tables to oneantother.

    Additionally I'd recommend this tutorial by Keith Brown.

    0 讨论(0)
  • 2021-01-22 19:57

    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'
    
    0 讨论(0)
  • 2021-01-22 20:01

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

    0 讨论(0)
  • 2021-01-22 20:02

    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'
    
    0 讨论(0)
提交回复
热议问题