MySql根据专栏加入

时间:2017-11-19 19:13:19

标签: mysql sql join

我想从报告终端表中选择行来创建如下所示的行:

+----+-------------------+---------------------+---------+-------+--------+-----+---------+------+
| id | mac_adresi        | zaman               | fabrika | kumes | makina | kat | sol_sag | adet |
+----+-------------------+---------------------+---------+-------+--------+-----+---------+------+
|  3 | 97-F9-2C-55-19-72 | 2017-11-18 22:43:29 | Çorum2  | Ana2  | 22     | 12  | So2     | 213  |
+----+-------------------+---------------------+---------+-------+--------+-----+---------+------+

报告表格;

+----+-------------------+------+---------------------+
| id | mac_adresi        | adet | zaman               |
+----+-------------------+------+---------------------+
|  3 | 97-F9-2C-55-19-72 | 213  | 2017-11-18 22:43:29 |
+----+-------------------+------+---------------------+

终端表;

+----+-------------------+---------+-------+--------+-----+---------+
| id | mac_adresi        | fabrika | kumes | makina | kat | sol_sag |
+----+-------------------+---------+-------+--------+-----+---------+
| 86 | 97-F9-2C-55-19-72 | Çorum2  | Ana2  | 22     | 12  | So2     |
+----+-------------------+---------+-------+--------+-----+---------+

终端表中,mac_adresi主键

报告表格中,mac_adresi外键

我尝试了这个查询,但结果不是我想要达到的目的。

SELECT report.*,terminal.fabrika,terminal.kumes,terminal.makina,terminal.kat,terminal.sol_sag FROM report JOIN terminal ORDER BY id DESC limit 10

我该怎么做?

1 个答案:

答案 0 :(得分:1)

你应该为join

设置ON子句
SELECT 
      report.*
      ,terminal.fabrika
      ,terminal.kumes
      ,terminal.makina
      ,terminal.kat
      ,terminal.sol_sag 
FROM report JOIN terminal on  report.mac_adresi = Terminal.mac_adresi   
ORDER BY id DESC limit 10