Mysql 加入并使用

Mysql JOIN and USING

我在 mysql 中遇到 JOIN 问题,我认为解决方案很简单,但我不知道如何实现。

mysql> SELECT * FROM inputs WHERE name_competition = 'ROBOTOVANI';
+----------+------------------+------------+----------------+
| id_input | name_competition | name_input | data_type      |
+----------+------------------+------------+----------------+
|       53 | ROBOTOVANI       | Id         | unique_numbers |
|       54 | ROBOTOVANI       | Jmeno      | unique_words   |
|       55 | ROBOTOVANI       | Prvni_cas  | times          |
+----------+------------------+------------+----------------+

mysql> SELECT * FROM unique_numbers WHERE name_competition = 'ROBOTOVANI';
+------------------+------------------+------------+-----------+-------+
| id_unique_number | name_competition | name_input | id_record | value |
+------------------+------------------+------------+-----------+-------+
|               31 | ROBOTOVANI       | Id         |         7 |     7 |
|               32 | ROBOTOVANI       | Id         |        12 |    12 |
|               33 | ROBOTOVANI       | Id         |        15 |    15 |
+------------------+------------------+------------+-----------+-------+

mysql> SELECT * FROM unique_words WHERE name_competition = 'ROBOTOVANI';
+----------+------------------+------------+-----------+--------+
| id_words | name_competition | name_input | id_record | value  |
+----------+------------------+------------+-----------+--------+
|       13 | ROBOTOVANI       | Jmeno      |        12 | George |
|       12 | ROBOTOVANI       | Jmeno      |         7 | John   |
|       14 | ROBOTOVANI       | Jmeno      |        15 | Lucy   |
+----------+------------------+------------+-----------+--------+

mysql> SELECT * FROM times WHERE name_competition = 'ROBOTOVANI';
+---------+------------------+------------+-----------+----------+
| id_time | name_competition | name_input | id_record | value    |
+---------+------------------+------------+-----------+----------+
|       1 | ROBOTOVANI       | Prvni_cas  |        15 | 00:25:33 |
+---------+------------------+------------+-----------+----------+

我需要以下结果

+-----+--------+-----------+
|  ID | Jmeno  | Prvni_cas |
+-----+--------------------+
| 12  | George |  NULL     |
|  7  |  John  |  NULL     |
|  15 |  Lucy  |  00:25:33 |
+-----+--------+-----------+

列'id_record'是关键,它只是一个简单的例子。 我通常有更多的投入。

这应该可以在不包含更多表格的情况下为您提供所需的结果。

SELECT      uw.id_record as 'ID', uw.value as 'Jmeno', times.value as 'Prvni_cas'
FROM        unique_words uw
LEFT JOIN   times times
ON          times.id_record = uw.id_record
WHERE       uw.name_competition = 'ROBOTOVANI'
ORDER BY    uw.name_input ASC

要通过合并更多表格进行详细说明,您可以执行以下操作:

SELECT      * 
FROM        unique_words uw
LEFT JOIN   times times
ON          times.id_record = uw.id_record
LEFT JOIN   unique_numbers un
ON          un.id_record = uw.id_record
WHERE       uw.name_competition = 'ROBOTOVANI'
ORDER BY    uw.name_input ASC

我留下了星号,这样您就可以看到在添加多个表时所有数据是如何呈现的。