我的数据库中有以下两个表:
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 |
-------------------------------------------------------------------
最佳答案
利用 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 ║
╚════════════╩══════════════╩═══════════════╩═══════════════════╝
关于php - MySQL + PHP : Select all results from one table for each result of another,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15470037/