Php 将同一个表的多行中的一个字段分组在一个表中,可以得到Mysql和无脂肪框架

Php 将同一个表的多行中的一个字段分组在一个表中,可以得到Mysql和无脂肪框架,php,mysql,fat-free-framework,Php,Mysql,Fat Free Framework,我在MySQL中有三个表: CUSTOMERS +------------+--------------+ | customerId | customerName | +------------+--------------+ PRODUCTS +-----------+-------------+ | productId | productName | +-----------+-------------+ RENTALS +--------------+--------------+--

我在MySQL中有三个表:

CUSTOMERS
+------------+--------------+
| customerId | customerName |
+------------+--------------+

PRODUCTS
+-----------+-------------+
| productId | productName |
+-----------+-------------+

RENTALS
+--------------+--------------+-----------------+
| rentalNumber | rentalAmount | rentalProductId |
+--------------+--------------+-----------------+
Rentals表中有一个rentalNumber的不同行。 我需要在php中返回如下结果:

RESULT
+--------------+--------------+----------------------------------+
| customerName | rentalNumber | rentalDetails                    |
+--------------+--------------+----------------------------------+
| Johnny       | 20           | productName1 x productAmount1,   |
|              |              | productName2 x productAmount 2,  |
|              |              | productName3 x productAmount 3   |
+--------------+--------------+----------------------------------+

rentalDetails位可能是一个字符串,显示在HTML表格中。

在不断摸索的过程中,我最终找到了答案:

SELECT *, group_concat(concat(`productName`,' x ',`rentalProductAmount`) separator ',') AS items
FROM rentals
LEFT JOIN customers on rentals.rentalCustomer = customers.customerId
LEFT JOIN products ON rentals.rentalProduct = products.productId
WHERE rentals.rentalStartDate >= NOW()
GROUP BY rentals.rentalNumber

但也许还有更好的办法。这对我来说很有用:)

那么到目前为止你尝试了什么?提示:使用group concatI尝试加入我可以做的表格;)和组_concat混在一起,但我无法理解…请向我们展示您尝试过的SQL查询!感谢jquery.PHP.Magento.com,我最终通过修改找到了答案:)