简体   繁体   中英

Left join with left table values with select query

I want a query where I can show the left table value with in select query.

Basically Left column have column A having same value but column B having different values so group_concat the column b value in the left table.

In the below query I am getting title of each in a separate row but I want it in same row against the commerce_order_item.order_id because commerce_order_item.order_id containe same value so want to group_concat(commerce_order_item.title)

 SELECT commerce_order.order_id, commerce_order.mail, commerce_order.total_price__number, commerce_order.changed, commerce_order_item.title FROM commerce_order LEFT JOIN commerce_order_item ON commerce_order.order_id = commerce_order_item.order_id WHERE cart = 1 AND commerce_order.changed BETWEEN $startdate AND $endates

Below is the query to group_concat

SELECT order_id, GROUP_CONCAT(title) FROM commerce_order_item GROUP BY order_id;

Resultant query

SELECT commerce_order.order_id, commerce_order.mail, commerce_order.total_price__number, commerce_order.changed, commerce_order_item.title FROM commerce_order LEFT JOIN commerce_order_item ON commerce_order.order_id = commerce_order_item.order_id WHERE cart = 1 AND commerce_order.changed BETWEEN 1640998861 AND 1641258061 AND commerce_order_item.title = (SELECT GROUP_CONCAT(commerce_order_item.title) FROM commerce_order_item GROUP BY commerce_order_item.order_id)

Here are sample table:

commerce_order

commerce_order tabe as below
+--------+---------------+--------------+-----------+
|order_id|       mail|   | Total Price  | changed   |
+--------+---------------+------+-------+-----------+
|    1   |abc@gmail.com  |      1000    |1641276265 |   
|    2   |abc1@gmail.com |      5000    |1641276266 |
|    3   |abc2@gmail.com |      100     |1641276267 |
|    4   |abc3@gmail.com |      1001    |1641276268 |
|    5   |abc4@gmail.com |      10000   |1641276269 |

commerce_order_item table as below

+--------+-------+
|order_id| title |
+--------+-------+
|    1   |   abc |
|    1   |   xyz |
|    1   |   def |
|    2   |   ghi |
|    2   |   lmn |

Result should be:
Order Id | Mail           | total Price | Time(timestamp)| title 

1         abc@gmail.com    1000          1641276265         abc,xyz,def
2         abc1@gmail1.com  5000          1641276266         ghi,lmn 
 

Does this not give you what you want?

SELECT commerce_order.order_id
, commerce_order.mail
, commerce_order.total_price__number
, commerce_order.changed
, GROUP_CONCAT(commerce_order_item.title)
FROM commerce_order 
LEFT JOIN commerce_order_item ON commerce_order.order_id = commerce_order_item.order_id 
WHERE cart = 1 
AND commerce_order.changed BETWEEN 1640998861 AND 1641258061 
GROUP BY commerce_order.order_id
, commerce_order.mail
, commerce_order.total_price__number
, commerce_order.changed

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM