mysql/ sql/ select/ merge/ row

I know there's a lot of posts about this, but I just don't don't understand how to do this?

This is the code I'm using (no merging here):

SELECT coupon_code, order_id, youwp_woocommerce_order_itemmeta.meta_value, youwp_postmeta.meta_value  

FROM `youwp_woocommerce_order_items`, `youwp_woocommerce_order_itemmeta`, `youwp_postmeta`, `you_ambassador`  

WHERE you_ambassador.user_id = 1
AND youwp_woocommerce_order_items.order_item_name = you_ambassador.coupon_code
AND order_id = youwp_postmeta.post_id
AND youwp_woocommerce_order_items.order_item_id = youwp_woocommerce_order_itemmeta.order_item_id
AND youwp_woocommerce_order_itemmeta.meta_key = 'discount_amount'
AND (youwp_postmeta.meta_key = '_billing_first_name' OR youwp_postmeta.meta_key = '_billing_last_name')

Which gives me:

Query results

And I want to merge/combine the meta_value data "Kennet Leth" and "Sørensen" which is:

youwp_postmeta.meta_key = '_billing_first_name'
youwp_postmeta.meta_key = '_billing_last_name'

I found some answers to similar questions using STUFF or MAX and GROUP BY, but I just don't understand the syntax?

It's when the answers use selections like t1 and t2 or A and B (t1.value - t2.value or A.value = B.value) that I just don't get?
I've tried to change these solutions so they will fit my code, but just can't get it to work:

SELECT STUFF((SELECT ', ' + A.[Bank Account No] FROM tbl_Customer_Bank_Details A
Where A.[Customer ID]=B.[Customer ID] FOR XML PATH('')),1,1,'') As [Bank Accounts]
From tbl_Customer_Bank_Details B
Group By [Customer ID], [Customer Name]

or

SELECT Code,
   ( SELECT Name + ' '
       FROM Table1 t2
      WHERE t2.Code = t1.Code
      ORDER BY Name
        FOR XML PATH('') ) AS Name
  FROM Table1 t1
  GROUP BY Code ;

How do I put in my table and column values?

EDIT:
To clarify I'm trying to merge 2 rows/results from the column ´youwp_postmeta.meta_value´.
I get the results from specifying:

WHERE (youwp_postmeta.meta_key = '_billing_first_name'  
OR youwp_postmeta.meta_key = '_billing_last_name')
AND order_id = youwp_postmeta.post_id

so I get 2 rows for each order_id, one for _billing_first_name and one for _billing_first_name and these are the rows I want to merge/join.

Found a way to do it ;)

Just changed my SELECT string to include underqueries and CONCAT():

SELECT coupon_code, order_id, im.meta_value, 
CONCAT((SELECT pm.meta_value FROM `youwp_postmeta` AS pm WHERE pm.meta_key = 
'_billing_first_name' AND order_id = pm.post_id), ' ',
(SELECT pm.meta_value FROM `youwp_postmeta` AS pm WHERE pm.meta_key = 
'_billing_last_name' AND order_id = pm.post_id)) AS name

FROM 
`youwp_woocommerce_order_items` AS i,
`youwp_woocommerce_order_itemmeta` AS im,
`youwp_postmeta` AS pm,
`you_ambassador` AS a

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Merging sql rows with same values Merging multiple rows with same field in column Merging similar SQL rows My Sql merging rows SQL select rows with the same column's value SQL rows sharing same column value and with where how to merge rows with the same column in sql like this? Count multiple rows with same column in SQL Issue with merging of rows in sql server SQL merging rows differing by 1 parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM