我的数据库中有以下两个表:
TABLE 1: Specials
---------------------------------------------
| Special ID | Special Name | Special Image |
--------------------------------------------
| 1 | Special 1 | 1.jpg |
| 2 | Special 2 | 2.jpg |
| 3 | Special 3 | 3.jpg |
---------------------------------------------
TABLE 2: Special Items
---------------------------
| Item ID | Item Name |
---------------------------
| 1 | Service |
| 2 | Clean |
| 3 | Dry |
---------------------------
从数据库中提取时,我正在寻找实现此目的的方法(第4列将是下拉列表):
EDIT SPECIALS (Columns 1-3: Table 1) (Column 4: All results from Table 2)
-------------------------------------------------------------------
| Special ID | Special Name | Special Image | Items |
-------------------------------------------------------------------
| 1 | Special 1 | 1.jpg | Service, Clean, Dry |
| 2 | Special 2 | 2.jpg | Service, Clean, Dry |
| 3 | Special 3 | 3.jpg | Service, Clean, Dry |
-------------------------------------------------------------------
答案 0 :(得分:4)
使用能够连接行的MySQL GROUP_CONCAT()
方法。
SELECT a.*,
(SELECT GROUP_CONCAT(ItemName) FROM SpecialItems) ItemList
FROM Specials a
输出
╔════════════╦══════════════╦═══════════════╦═══════════════════╗
║ SPECIAL ID ║ SPECIAL NAME ║ SPECIAL IMAGE ║ ITEMLIST ║
╠════════════╬══════════════╬═══════════════╬═══════════════════╣
║ 1 ║ Special 1 ║ 1.jpg ║ Service,Clean,Dry ║
║ 2 ║ Special 2 ║ 2.jpg ║ Service,Clean,Dry ║
║ 3 ║ Special 3 ║ 3.jpg ║ Service,Clean,Dry ║
╚════════════╩══════════════╩═══════════════╩═══════════════════╝
答案 1 :(得分:1)
使用GROUP_CONCATE
MySQL帮助您将结果与逗号分隔结合起来。
<强> Example
强>