I’m having difficulty removing the duplicate data when exporting an order where more than 1 product is ordered.
Example:
orders_id, product_id, customer_first_name, customer_last_name
001 , ProductA , FirstName , LastName
001 , ProductB , FirstName , LastName
001 , ProductC , FirstName , LastName
002 , ProductA , FirstName , LastName
003 , ProductB , FirstName , LastName
003 , ProductC , FirstName , LastName
004 , ProductA , FirstName , LastName
I need to remove the duplicating data, like so:
orders_id, product_id, customer_first_name, customer_last_name
001 , ProductA , FirstName , LastName
, ProductB , ,
, ProductC , ,
002 , ProductA , FirstName , LastName
003 , ProductB , FirstName , LastName
, ProductC , ,
004 , ProductA , FirstName , LastName
I tried DISTINCT, without any change.
Assuming the query to extract the above is as follows:
SELECT
o.orders_id
, op.product_id
, o.customer_first_name
, o.customer_last_name
FROM orders AS o
, orders_products AS op
WHERE o.orders_id = op.orders_id;
How would I limit the data from duplicating?
Much appreciated!
Peace,
Chris
This is a display problem and not an SQL one. It could be solved with SQL but the query would be rather horrible (in MySQL).
One way to solve is to change the display a bit by using @Bauhaus advice and
GROUP_CONCAT()function.Another way would be to change the application code to combine the results of two queries: