【问题标题】:How to get data from 3 tables in one SELECT [MySQL]如何在一个 SELECT [MySQL] 中从 3 个表中获取数据
【发布时间】:2023-04-03 10:05:01
【问题描述】:

我有 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 cases WHERE vin = :vin ORDER BY date_created DESC LIMIT 60";

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

    "SELECT * FROM 计算 WHERE case_id = :case AND country = :country ORDER BY calculate_id DESC LIMIT 1";

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

    “从位置中选择文本计算 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 的选择,等等。我错过了什么吗?

感谢您的帮助。

【问题讨论】:

  • 您的预期结果是什么?
  • 嗨 @ZoharPeled 问题已编辑,我可以通过 PHP 存储变量来获得该结果,我希望跳过此

标签: php mysql


【解决方案1】:

试试这个:

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

【讨论】:

  • 嗨,Zohar,我只是检查一下代码确实看起来不错并且有意义,当我使用此 SQL 时,我没有收到任何错误,但输出为空白 :(
  • 有效!我没有得到任何结果,因为在我的职位上我对这个声明没有任何结果。我将不得不弄清楚它,但仍然非常感谢!为您提供帮助
猜你喜欢
  • 1970-01-01
  • 2014-01-22
  • 2016-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多