我在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表格中。
答案 0 :(得分:0)
在摆弄的同时,我终于找到了答案:
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
但也许还有更好的方法。这虽然适用于我:)