简体   繁体   English

mysql - 查询三个表

[英]mysql - query three tables

I have a relational database with three tables. 我有一个有三个表的关系数据库。 The first containts id's that relate to the second. 第一个包含与第二个相关的id。 The second contains id's that relate to the third. 第二个包含与第三个相关的id。 The third contains the results I am after. 第三个包含我追求的结果。

Is it possible with a single query to query an id in the first table which gives all results from the third table that relate to it? 是否可以使用单个查询来查询第一个表中的id,该表给出了与第三个表相关的所有结果?

Sorry I am new to mySQL. 对不起,我是mySQL的新手。

Mysql Join Mysql加入

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子句以搜索table1中的某些行

where t1.field = 'value'

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. 使用JOIN命令将表链接到另一个。

Additionally I'd recommend this tutorial by Keith Brown. 另外我推荐Keith Brown的这个教程

You want to do a join . 你想做一个加入 There is a lot of tutorials about this and various ways of doing it. 有很多关于此的教程各种方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM