如何从一张表中的 3 个表中获取数据 SELECT [MySQL]

How to get data from 3 tables in one SELECT [MySQL]

我有 3 个不同的 tables,其中基于第一个搜索 TABLE 我能够从第二个中获取数据,并且基于第一个和第二个中的搜索我能够从中获取数据第 3 table.

表格如下所示:

table cases
+-------+----------+------------+---------+
|case_id| car_model|     vin    | country |
+-------+----------+------------+---------+
|  1    |    VW    |   ABCDEFG  |    EN   |
+-------+----------+------------+---------+
|  2    |    VW    |   GFEDCBA  |    PL   |
+-------+----------+------------+---------+
table calculations
+-------+---------------+------------+---------+
|case_id|calculation_id |    price   | country |
+-------+---------------+------------+---------+
|   1   |45545662512    |    11000   |    EN   |
+-------+---------------+------------+---------+
|   1   |45545662512    |     7000   |    PL   |
+-------+---------------+------------+---------+
|   2   |1234561234     |     3000   |    EN   |
+-------+---------------+------------+---------+
|   2   |3214561234     |     6000   |    EN   |
+-------+---------------+------------+---------+
table positions
+-------+------------+------------+--------------+
|case_id|repairmethod|    text    |calculation_id|
+-------+------------+------------+--------------+
|   1   |     L      |    hallo   |  7894561234  |
+-------+------------+------------+--------------+
|   1   |     L      |     hi1    |  45545662512 |
+-------+------------+------------+--------------+
|   2   |     L      |     hi2    |  45545662512 |
+-------+------------+------------+--------------+
|   2   |     E      |     hi3    |  45545662512 |
+-------+------------+------------+--------------+
|   2   |     G      |     hi4    |  45545662512 |
+-------+------------+------------+--------------+
|   2   |     L      |     hi5    |  3214561234  |
+-------+------------+------------+--------------+
|   2   |     L      |     hi6    |  3214561234  |
+-------+------------+------------+--------------+

我是怎么写的:

例如用户搜索 ABCDEFG 时的预期结果:

来自 table 个案例:

car_model = VW
country = EN
case_id = 1

根据table计算:

calculation_id = 45545662512    
price = 11 000

来自 table 个位置:

text = hi1
text = hi2

但是这个查询是基于PHP的,将值存储在变量中等等,有没有机会把我以前的所有语句写在一个SELECT中?

我已经尝试使用 INNER JOIN

"SELECT * FROM cases v INNER JOIN calculations c
                    ON v.case_id = c.case_id
                    INNER JOIN positions p
                    ON v.case_id = p.case_id
                    WHERE v.vin = :vin
                    ORDER BY c.name DESC
                    LIMIT 4";

但是有多个问题,有限制,select 有多个 WHERE,等等。我错过了什么吗?

感谢您的帮助。

试试这个:

SELECT cases.case_id, cases.car_model, cases.country, 
       calculations.calculation_id, calculations.price, 
       positions.text 
FROM cases INNER JOIN
calculations ON(cases.case_id = calculations.case_id AND cases.country = calculations.country) INNER JOIN
positions ON(calculations.case_id = positions .case_id AND calculations.calculation_id = positions.calculation_id)
WHERE vin = :vin 

注意:如果您搜索 'ABCDEFG',这将为您提供 2 行数据,每行对应位置 table.

中的每条记录