mysql - 仅从数据库中更改一列即可获取同一行的多行

标签 mysql laravel-5

我正在使用以下查询从数据库中获取记录。它与许多其他表相连。一切工作正常,但问题是我得到了多行相同的预订,这是因为预订表已与付款表连接,而付款表针对一个预订有多行,这就是它重复完整记录的原因。我想要的是显示包含所有付款行的单行,以便我可以循环遍历所有付款并显示它们,而不是一次又一次地显示整个预订记录。以下是我的查询。这是 Laravel 5.2。

return $results = DB::table('resorts_reservation')
            ->join('resorts_resort', 'resorts_reservation.resortId', '=', 'resorts_resort.resortId')
            ->join('resorts_roomtype', 'resorts_reservation.roomTypeId', '=', 'resorts_roomtype.roomTypeId')
            ->join('resorts_customer', 'resorts_reservation.customerId', '=', 'resorts_customer.customerId')
            ->join('resorts_salesperson', 'resorts_reservation.salesPersonId', '=', 'resorts_salesperson.salesPersonId')
            ->join('resorts_payment', 'resorts_reservation.reservationId', '=', 'resorts_payment.reservationId')
            ->join('resorts_paymentmethod', 'resorts_payment.paymentMethodId', '=', 'resorts_paymentmethod.paymentMethodId')
            ->join('resorts_emailnotification', 'resorts_reservation.reservationId', '=', 'resorts_emailnotification.reservationId')
            ->join('resorts_resortcompany', 'resorts_resort.resortCompanyId', '=', 'resorts_resortcompany.resortCompanyId')
            ->select('resorts_reservation.totalAmount', 'resorts_reservation.saleDate', 'resorts_reservation.reservationId', 
                    'resorts_reservation.confirmNo', 'resorts_reservation.numberOfNights', 'resorts_reservation.checkInDate', 
                    'resorts_reservation.checkOutDate', 'resorts_reservation.numberOfAdults', 'resorts_reservation.numberOfChildren',
                    'resorts_reservation.totalInParty', 'resorts_reservation.notes', 'resorts_reservation.totalPrice',
                    'resorts_reservation.saleSource', 'resorts_reservation.depositAmount', 'resorts_reservation.confirmationSent',
                    'resorts_reservation.finalized',
                    'resorts_customer.firstName', 'resorts_customer.mobilePhone', 'resorts_customer.otherPhone',
                    'resorts_customer.email', 'resorts_customer.addressLineOne', 'resorts_customer.city',
                    'resorts_customer.country', 'resorts_customer.state_', 'resorts_customer.postalCode',
                    'resorts_resort.resortName', 
                    'resorts_roomtype.roomTypeDesc', 'resorts_roomtype.occupancy',
                    'resorts_salesperson.firstName as saleFirstName','resorts_salesperson.lastName as saleLastName',
                    'resorts_paymentmethod.methodType',
                    'resorts_payment.transactionNo',
                    'resorts_resortcompany.resortCompanyName')
            ->whereRaw($where)
            ->orderBy('reservationId', 'desc')
            ->get();

下面是查询返回的结果。您可以看到两行都有相同的记录,只是“transactionNo”列发生了变化。

[0] => stdClass Object
    (
        [totalAmount] => 2161.2
        [saleDate] => 2016-03-09 00:00:00
        [reservationId] => 30286
        [confirmNo] => 
        [numberOfNights] => 3
        [checkInDate] => 2016-04-22 00:00:00
        [checkOutDate] => 2016-04-25 00:00:00
        [numberOfAdults] => 6
        [numberOfChildren] => 0
        [totalInParty] => 6
        [notes] => 
        [totalPrice] => 2161.2
        [saleSource] => VRBO
        [depositAmount] => 2161.2
        [confirmationSent] => 0
        [finalized] => 0
        [firstName] => Michael
        [mobilePhone] => 505-321-2106
        [otherPhone] => 
        [email] => xxxxxxxx@gmail.com
        [addressLineOne] => 
        [city] => Albuquerque
        [country] => USA
        [state_] => NM
        [postalCode] => 87111
        [resortName] => San Francisco - Canterbury
        [roomTypeDesc] => 3 Bedroom Presidential
        [occupancy] => 6
        [saleFirstName] => Kristy
        [saleLastName] => Conlin
        [methodType] => CREDIT_CARD
        [transactionNo] => 7MG983973K453254C

        [resortCompanyName] => Wyndham
    )

[1] => stdClass Object
    (
        [totalAmount] => 2161.2
        [saleDate] => 2016-03-09 00:00:00
        [reservationId] => 30286
        [confirmNo] => 
        [numberOfNights] => 3
        [checkInDate] => 2016-04-22 00:00:00
        [checkOutDate] => 2016-04-25 00:00:00
        [numberOfAdults] => 6
        [numberOfChildren] => 0
        [totalInParty] => 6
        [notes] => 
        [totalPrice] => 2161.2
        [saleSource] => VRBO
        [depositAmount] => 2161.2
        [confirmationSent] => 0
        [finalized] => 0
        [firstName] => Michael
        [mobilePhone] => 505-321-2106
        [otherPhone] => 
        [email] => xxxxxxxx@gmail.com
        [addressLineOne] => 
        [city] => Albuquerque
        [country] => USA
        [state_] => NM
        [postalCode] => 87111
        [resortName] => San Francisco - Canterbury
        [roomTypeDesc] => 3 Bedroom Presidential
        [occupancy] => 6
        [saleFirstName] => Kristy
        [saleLastName] => Conlin
        [methodType] => CREDIT_CARD
        [transactionNo] => 
        [resortCompanyName] => Wyndham
    )

我想要的是如下所示:

[0] => stdClass Object
    (
        [totalAmount] => 2161.2
        [saleDate] => 2016-03-09 00:00:00
        [reservationId] => 30286
        [confirmNo] => 
        [numberOfNights] => 3
        [checkInDate] => 2016-04-22 00:00:00
        [checkOutDate] => 2016-04-25 00:00:00
        [numberOfAdults] => 6
        [numberOfChildren] => 0
        [totalInParty] => 6
        [notes] => 
        [totalPrice] => 2161.2
        [saleSource] => VRBO
        [depositAmount] => 2161.2
        [confirmationSent] => 0
        [finalized] => 0
        [firstName] => Michael
        [mobilePhone] => 505-321-2106
        [otherPhone] => 
        [email] => xxxxxxxx@gmail.com
        [addressLineOne] => 
        [city] => Albuquerque
        [country] => USA
        [state_] => NM
        [postalCode] => 87111
        [resortName] => San Francisco - Canterbury
        [roomTypeDesc] => 3 Bedroom Presidential
        [occupancy] => 6
        [saleFirstName] => Kristy
        [saleLastName] => Conlin
        [methodType] => CREDIT_CARD
        array(
            [transactionNo] => 7MG983973K453254C
            [transactionNo] => 
        )
        [resortCompanyName] => Wyndham
    )

最佳答案

您可以使用 GROUP-CONCAT 函数对每个预订的所有付款交易号码进行分组。

来自MySQL docs

This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values.

您的查询将如下所示:

SELECT resorts_reservation.totalAmount, ..., GROUP_CONCAT(resorts_payment.transactionNo separator ', ') as transactionNoList
WHERE...

希望这是有道理的,如果没有,我很乐意澄清。

汤姆·拉什曼

关于mysql - 仅从数据库中更改一列即可获取同一行的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36737640/

相关文章:

mysql - 我是sequelize的新手,如何将mysql查询转换为sequelize

mysql - 查询 : Tables with indexing/foregeing keys/correlated data

php - 为什么中间件不实现接口(interface)?

blade - Laravel 5在 Blade View 中调用模型函数

laravel - Laravel 中的多个选择中用逗号分隔每个值

PHP MySQL 将变量分配给数组数据

mysql - 如何对两个表使用 max

PHP: mysql_query 及其连接说明

php - Laravel Mysql 查询选择条件

php - 哪个版本的 Laravel 使用 LTS 或最新版本 - 至少 5 年的开发人员支持 promise ?