Mysql中group concat函式的妙用

2021-08-07 08:52:30 字數 2696 閱讀 4552

我們在查詢資料的時候,經常遇到一對多的表關係,對其進行多表關聯查詢,

但是在列表裡面顯示資料的時候需要根據一的表進行資料顯示,同時在一行內顯示多的資料。大概例子如下圖所示

這時候我們可以使用group_concat進行資料查詢的一些處理:
select

com.serial_number,

cis.customer_type,

cis.company_name,

cis.`name`,

cis.logo_pic,

cba.contract,

cba.telephone,

cba.province,

cba.city,

cba.area,

cba.address,

group_concat(

''),

com.pk_invoices,

com.total_num,

com.cost_num,

com.order_status,

com.real_num,

com.pk_order,

com.billing_status,

ccb.code_type,

ccb.bankcode

from

cti_order_message com

inner

join cti_bank_address cba on cba.pk_bank_address = com.pk_bank_address

inner

join cti_individual_customer cis on cis.pk_customer = com.pk_customer

left

join cti_customer_bankcode ccb on com.pk_bank_code = ccb.pk_bankcode

left

join cti_order_and_product coap on com.pk_order = coap.pk_order

left

join (

select

pm.pk_product,

pm.product_name,

pm.product_logo_pic,

cpd.discount_type,

cpd.enough_price,

cpd.sub_price,

cpd.enough_num,

cpd.give_product_num,

cpd.give_product_name,

cpd.give_product_logo_pic,

cpd.discount_price,pm.product_weight,pm.retail_price,cpc.catalog_name

from

cti_product_message pm

left

join (

select

cpd.pk_product,

cpd.discount_type,

cpd.enough_price,

cpd.sub_price,

cpd.enough_num,

cpd.give_product_num,

cpm.product_name as give_product_name,

cpm.product_logo_pic as give_product_logo_pic,

cpd.discount_price

from

cti_product_discount cpd

left

join cti_product_message cpm on cpd.give_pk_product = cpm.pk_product

where

cpd.dr = 0

and cpd.start_date <= '2017-08-22 13:16:00'

and cpd.end_date >= '2017-08-22 13:16:00'

and cpd.use_status = 1

and (

cpd.is_all_bank = 1

or (

is_all_bank = 2

and find_in_set(

'40289f9a5d7d9602015d7d9baf000000',

pkbank))

)) cpd on pm.pk_product = cpd.pk_product

left

join cti_product_catalog cpc on cpc.pk_catalog = pm.pk_catalog

) cpm on coap.pk_product = cpm.pk_product

where

com.dr = 0

and com.order_status = 1

and com.pk_order = '40289f9a5def7129015def728c310003'

group

by com.pk_order;

Mysql分組資料合併group concat用法

group concat用法 預設用法 select group concat vaccine name as do accinename from t vaccine where id in select vaccine id from t dog vaccine where dog id 1 1...

MySQL 多行資料合併 GROUP CONCAT

表結構及資料 drop table if exists course create table course id int 11 not null,name varchar 50 default null comment 姓名 course name varchar 50 default null ...

MySQL 多行資料合併 GROUP CONCAT

表結構及資料 drop table if exists course create table course name varchar 255 character set utf8 collate utf8 general ci default null,course name varchar 25...