Solved

4 table Join - Eliminate repeating rows

Posted on 2009-05-04
4
721 Views
Last Modified: 2012-05-06
I am trying to show customer name and address together with the products they purchased. Including product attributes (color etc.).
Example
Joe Rabbitte, 1- Hopper Ave,
1 - Wolly jumper - Red,
2 - Wolly Socks - white
(count removed for clarity)


This query works fine with the first two joins but returns the products twice in the products_column when the third join is added.

Joe Rabbitte, 1- Hopper Ave,
1- Wolly jumper - Red,
1- Wolly jumper - Red,
2- Wolly Socks - white,
2- Wolly Socks - white,

I'm stuck has anyone any ideas on what I am doing wrong?
SELECT customers_name, customers_telephone, 

GROUP_CONCAT(products_name, ' ', LEFT(products_options_values, 4) SEPARATOR ' - ') AS products_bought

FROM orders

LEFT JOIN orders_products ON orders.orders_id = orders_products.orders_id

LEFT JOIN cc ON orders_products.orders_id = cc.order_id

LEFT JOIN orders_products_attributes ON cc.order_id = orders_products_attributes.orders_id

WHERE cc.payment_status = 'completed' AND orders.orders_id >10000
 

GROUP BY customers_name

Open in new window

0
Comment
Question by:fearialtog
4 Comments
 
LVL 10

Assisted Solution

by:mahome
mahome earned 50 total points
ID: 24293661
This will happen if you have 2 rows in orders_products_attributes per order id.
0
 
LVL 2

Assisted Solution

by:yousaftahir
yousaftahir earned 50 total points
ID: 24293761
use distinct to get the distinct rows.
0
 
LVL 4

Accepted Solution

by:
pellep earned 150 total points
ID: 24293943
Using "distinct" is an option, but keep in mind that it's a very, very expensive operation, especially in your case where you construct the result by formula and strings. Basically, it has the compare each row returned lexigraphically with the current list of results. If your query returns even a medium sized resultset, you will likely see a significant performance-impact.

Agreeing with mahome, it seems this is more of an issue of data than query. For some reason, there appears to be two (or more) rows in the orders_products_attributes table for each order, even if the rows represent the same data (assuming here that the products_options_values column comes from the orders_products_attributes table). Either this is not intended, in which case it should be fixed and your query should run fine. If it IS intended than you may end up having to use distinct or dropping the orders_products_attributes portion from the query.

This is one of the inherent downsides to a normalized model and this pattern of attributes storage you're using - it makes this type of query more problematic.
0
 
LVL 10

Author Closing Comment

by:fearialtog
ID: 31577487
Thank you all for your help
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Query syntax 10 40
ODBC Question 3 30
DBF to ... Converter 5 40
constraint check 2 13
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now