MySQL Query成单个多表连接

时间:2018-01-14 00:51:58

标签: mysql join

最终目标是根据intent和slot属性获取tbl_output.output。我目前正在以编程方式处理它,如果可能的话,我想组合成一个查询。如果需要,我愿意重组任何表格。

SELECT id as intent_id FROM `tbl_intent` WHERE `name` = 'Car' 
SELECT id as slot_id1 FROM `tbl_slot` WHERE `name` = '2018' 
SELECT id as slot_id2 FROM `tbl_slot` WHERE `name` = 'Chevrolet'
SELECT id as slot_id3 FROM `tbl_slot` WHERE `name` = 'Corvette'

示例应该返回tbl_output ID 1字段“output”。

SELECT 
    *,  
    output_id
FROM xref_intent_slot
    LEFT JOIN tbl_slot slot1 ON xref_intent_slot.slot_id=slot1.id AND slot1.name='2018'
    LEFT JOIN tbl_slot slot2 ON xref_intent_slot.slot_id=slot2.id AND slot2.name='Chevrolet'
    LEFT JOIN tbl_slot slot3 ON xref_intent_slot.slot_id=slot3.id AND slot3.name='Corvette'
WHERE `intent_id` = (SELECT id from tbl_intent WHERE `name` = 'Car')


+----+-----------+-----------+---------+------+------+------+-----------+------+----------+-----------+
| id | output_id | intent_id | slot_id | id   | name | id   | name      | id   | name     | output_id |
+----+-----------+-----------+---------+------+------+------+-----------+------+----------+-----------+
|  1 |         1 |         1 |       1 |    1 | 2018 | NULL | NULL      | NULL | NULL     |         1 |
|  2 |         1 |         1 |       2 | NULL | NULL |    2 | Chevrolet | NULL | NULL     |         1 |
|  3 |         1 |         1 |       3 | NULL | NULL | NULL | NULL      |    3 | Corvette |         1 |
|  4 |         2 |         1 |       4 | NULL | NULL | NULL | NULL      | NULL | NULL     |         2 |
|  5 |         2 |         1 |       2 | NULL | NULL |    2 | Chevrolet | NULL | NULL     |         2 |
|  6 |         2 |         1 |       5 | NULL | NULL | NULL | NULL      | NULL | NULL     |         2 |
+----+-----------+-----------+---------+------+------+------+-----------+------+----------+-----------+

这应该只返回ID为1,2和3,即output_id 1.然后使用该值从tbl_output获取输出?

表:

+-------------------+
| tbl_intent        |
| tbl_output        |
| tbl_slot          |
| xref_intent_slot  |
+-------------------+

表:tbl_intent

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | NO   | MUL | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

+----+------+
| id | name |
+----+------+
|  1 | Car  |
+----+------+

表:tbl_slot

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | NO   | MUL | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

+----+-----------+
| id | name      |
+----+-----------+
|  1 | 2018      |
|  2 | Chevrolet |
|  3 | Corvette  |
|  4 | 2017      |
|  5 | Camaro    |
+----+-----------+

表:tbl_output

+--------+---------+------+-----+---------+----------------+
| Field  | Type    | Null | Key | Default | Extra          |
+--------+---------+------+-----+---------+----------------+
| id     | int(11) | NO   | PRI | NULL    | auto_increment |
| output | text    | NO   |     | NULL    |                |
+--------+---------+------+-----+---------+----------------+

+----+----------------+
| id | output         |
+----+----------------+
|  1 | Found Corvette |
|  2 | Found Camaro   |
+----+----------------+

表:xref_intent_slot

+-----------+---------+------+-----+---------+----------------+
| Field     | Type    | Null | Key | Default | Extra          |
+-----------+---------+------+-----+---------+----------------+
| id        | int(11) | NO   | PRI | NULL    | auto_increment |
| output_id | int(11) | NO   |     | NULL    |                |
| intent_id | int(11) | NO   |     | NULL    |                |
| slot_id   | int(11) | NO   |     | NULL    |                |
+-----------+---------+------+-----+---------+----------------+

+----+-----------+-----------+---------+
| id | output_id | intent_id | slot_id |
+----+-----------+-----------+---------+
|  1 |         1 |         1 |       1 |
|  2 |         1 |         1 |       2 |
|  3 |         1 |         1 |       3 |
|  4 |         2 |         1 |       4 |
|  5 |         2 |         1 |       2 |
|  6 |         2 |         1 |       5 |
+----+-----------+-----------+---------+

1 个答案:

答案 0 :(得分:0)

SELECT 
    `output`
FROM tbl_output
WHERE
    id =
    (
        SELECT  
            output_id
        FROM xref_intent_slot
            LEFT JOIN tbl_slot slot1 ON xref_intent_slot.slot_id=slot1.id AND slot1.name='2018'
            LEFT JOIN tbl_slot slot2 ON xref_intent_slot.slot_id=slot2.id AND slot2.name='Chevrolet'
            LEFT JOIN tbl_slot slot3 ON xref_intent_slot.slot_id=slot3.id AND slot3.name='Corvette'
        WHERE `intent_id` = (SELECT id from tbl_intent WHERE `name` = 'Car') AND
            (slot1.name = '2018' OR
            slot2.name = 'Chevrolet' OR
            slot3.name = 'Corvette')
        group by output_id
        HAVING (count(output_id) = 3)
    )
LIMIT 0,1
相关问题