Конечная цель состоит в том, чтобы получить 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 |
+----+-----------+-----------+---------+------+------+------+-----------+------+----------+-----------+
Это должно возвращать только идентификаторы 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 |
+----+-----------+-----------+---------+
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