简体   繁体   中英

How do I change SQL query output?

I'm using this simple SQL query to retrieve data from two tables in my MySQL database:

SELECT customer_id, domain_name
FROM customers_orders
INNER JOIN orders ON order_id = sub_id

The result is similar to this:

1114    somedomain.com
1115    anotherdomain.net
1116    domain1.org
1116    domain2.com

How do I tell it to give me an output that is rather similar to:

1114    somedomain.com
1115    anotherdomain.net
1116    domain1.org, domain2.com

So as to avoid having two lines with the same customer_id... I wonder if it is even possible with SQL? I'm a beginner in SQL and I would very much appreciate your help on this.

Thanks a lot!

Try somethin like this

SELECT customer_id, GROUP_CONCAT(domain_name SEPARATOR ',')
FROM customers_orders
INNER JOIN orders ON order_id = sub_id
GROUP BY customer_id

You can use this:

SELECT customer_id, GROUP_CONCAT(domain_name ORDER BY domain_name SEPARATOR ', ') domain_name
FROM customers_orders INNER JOIN orders 
ON order_id = sub_id
GROUP BY customers_orders.customer_id

Try next:

select customer_id, group_concat(domain_name separator ', ')
    from customers_orders group by customer_id

No, it isn't . It is possible via GROUP_CONCAT , as suggested by others. But I wouldn't advise to use it. It is better to handle this in your processing code. It will be easier to read and maintain.

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