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

时间:2015-04-08 15:22:54

标签: php mysql

我有3个不同的表,基于第一个表中的搜索,我能够从第二个获取数据,并且根据第一个和第二个搜索,我能够从第3个表中获取数据。

表格如下:

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  |
+-------+------------+------------+--------------+

我是怎么写的:

  • 从表号1获取数据和case_id:

    “SELECT * FROM case WHERE vin =:vin ORDER BY date_created DESC LIMIT 60”;

  • 基于case_id,我从表2中的第一个表搜索中获取,取最后一行:

    “SELECT * FROM computation WHERE case_id =:case AND country =:country ORDER BY calculation_id DESC LIMIT 1”;

  • 根据表1中的case_id和表2中的calculation_id在表3中搜索:

    “SELECT text FROM positions WHERE calculation_id =:calculationid AND case_id =:case_id AND repairmethod LIKE'L%'LIMIT 60”;

如果用户搜索ABCDEFG,则预期结果为:

来自表格案例:

car_model = VW
country = EN
case_id = 1

从表格计算:

calculation_id = 45545662512    
price = 11 000

从表位:

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";

但是有多个问题,有限制,选择多个WHERE,依此类推。我想念一下吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

试试这个:

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行数据,每个记录位置表中有一行。