MySQL - 从另一个表加入变量/动态行数

时间:2015-07-08 19:00:12

标签: mysql sql join

我有3张桌子。

+---------+
|  items  |
+---------+
| item_id |
| ...     |
+---------+

+----------------------+
|   item_text_values   |
+----------------------+
| item_text_value_id   |
| item_id              |
| custom_field_name_id |
| value                |
+----------------------+

+----------------------+
|  custom_field_names  |
+----------------------+
| custom_field_name_id |
| index                |
| name                 |
+----------------------+

item_text_values的计数未知。它可以是3个或10个或更多。我想有一个视图或存储过程,它返回带有变量列的行,具体取决于有多少个item_text_values记录。

我有经典的加入

SELECT *
 FROM items
LEFT JOIN item_text_values ON item_text_values.item_id = items.item_id
LEFT JOIN custom_field_names ON custom_field_names.custom_field_name_id = item_text_values.custom_field_name_id
WHERE items.item_id = 1

如果有2条item_text_values记录,则返回2行。 我希望将这两行合并为一个具有可变列的行,具体取决于有多少item_text_values。不知道这是否可能。

谢谢

0 个答案:

没有答案