我在MySQL中有四个表。
+-----------------------------------------------+
Table 2 | Table 1 |
+============+=========+========+============+ +==========+=====================+
| ID | Name | Add | d_id | | ID | Details |
+============+=========+========+============+ +==========+=====================+
| 1 | ABC | City | 3 | | 1 | blah blah blah |
+------------+---------+--------+------------+ +----------+---------------------+
: : : : : : : :
|
+-------------------------+
|
Table 3 | Table 4
+============+==========+============+ +=========+======================+
| d_id | dis_Name | cat_id |----+ | cat_id | category_name |
+============+==========+============+ | +=========+======================+
| 1 | Myeloma | 5 | | : : :
+------------+----------+------------+ | +---------+----------------------+
: : : : +----| 5 | Cancer |
+---------+----------------------+
我想要做的是,我想选择“表1”中属于“表4”中“category_name”的所有行。 表2可以包含具有相同“d_id”的多个行。同样,表3可能具有多个具有相同“cat_id”的行。 如果我选择“癌症”类别,那么我将从结果中获得表3中的多个d_id。对于每个d_id,表2中将有多个ID。我想从表1中获取这些ID的详细信息(来自表2)。查询语句是什么?
连接性显示在表格表示中。
答案 0 :(得分:1)
您需要的是加入。
SELECT table.column_in_table
FROM table
INNER JOIN table_with_same_values
ON table.column_in_table = table_with_same_values.column_in_that_table;
我还不清楚你要问的是什么。你的问题有点冗长。其他一些信息here。
希望这有帮助!
答案 1 :(得分:1)
你可以做到这一点:
SELECT t1.*
FROM t4
INNER JOIN t3 ON (t4.cat_id = t3.cat_id)
INNER JOIN t2 ON (t3.d_id = t2.d_id)
INNER JOIN t1 ON (t2.id = t1.id)
WHERE t4.category_name = 'Cancer';