简体   繁体   English

将同一表的几行中的一个字段分组为一个结果在 Mysql 和 Fat Free Framework 中

[英]Grouping one field from several rows of the same table in one result in Mysql and Fat Free Framework

I have three tables in MySQL:我在 MySQL 中有三个表:

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

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

RENTALS
+--------------+--------------+-----------------+
| rentalNumber | rentalAmount | rentalProductId |
+--------------+--------------+-----------------+

The Rentals table has various rows for one rentalNumber. Rentals 表为一个rentalNumber 包含不同的行。 I need to return a result in php like this:我需要像这样在 php 中返回一个结果:

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

the rentalDetails bit may be a string, displayed in a HTML table. RentalDetails 位可能是一个字符串,显示在 HTML 表中。

While fiddling along I found the answer eventually:在摆弄的过程中,我最终找到了答案:

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

But perhaps there's an even better way.但也许还有更好的方法。 This works for me though :)不过这对我有用:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Fat Free Framework $ mapper中,如何从$ mapper-> get()返回数据时在一个字段中修改数据 - In the Fat Free Framework $mapper, how can I modify the data in in one field as it is returned from $mapper->get() MySQL从一列到回显表分组相同的值 - mysql grouping same value from one column to echo table 无脂肪框架-防止将表名插入映射器对象的虚拟字段中 - Fat-Free-Framework - Prevent table name from being inserted into mapper object's virtual field 通过php将一个表中的一个字段与mysql中花药表中的几个字段进行比较 - Compare one field from one table to several fields in anther table in mysql via php 多个域和一个会话的无胖框架 - Multiple domains and one session fat-free framework MySQL:基于同一表的一条记录中一个选定字段中的值删除表中的多行 - MySQL: Delete multiple rows in a table based on a value in one selected field in one record of the same table 使用从表中获取的无脂肪框架 - Using fat-free-framework fetch from table MySQL-从另一个表中查询与当前表相同的ID的字段 - mySQL - Querying a field from another table with the same id as in the current one 如何从具有相同catID的表中获得一个结果,并从另一个表中获得多个结果? - How to get one result from a table and several from another with the same catID? MySQL从同一表中选择对多行分组 - MySQL select grouping multiple rows from same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM