Solved

MySQL Select Distinct on Left Join Problem

Posted on 2006-11-14
7
3,524 Views
Last Modified: 2012-05-05
I have 2 tables, one for groups, the other for items.  Multiple items are assigned to each group.

I am attempting to join both tables and select only unique/distinct groups based on the group ID.

SELECT DISTINCT DISTINCT GRP.id, GRP.description, ITM.gid, ITM.mfgnum
FROM products_groups AS GRP
LEFT JOIN products_items AS ITM ON GRP.id = ITM.gid
WHERE GRP.description NOT LIKE '%-%'
AND (
GRP.description LIKE '%engagement%'
OR GRP.description LIKE '%wedding%'
)
AND GRP.orderable = 'Y'
ORDER BY GRP.id DESC


Unfortunately, this still returns duplicates:

id | description | gid | mfgnum

12480 | 14k 8mm Tapered Wedding Band | 12480 | HRT080-10
12480 | 14k 8mm Tapered Wedding Band | 12480 | HRT080-6
12479 | 14k 6mm Tapered Wedding Band | 12479 | HRT060-10
12479 | 14k 6mm Tapered Wedding Band | 12479 | HRT060-6
12478 | 14k 5mm Tapered Wedding Band | 12478 | HRT050-10
12478 | 14k 5mm Tapered Wedding Band | 12478 | HRT050-6
12477 | 14k 4mm Tapered Wedding Band | 12477 | HRT040-10

This SHOULD only be returning:

12480 | 14k 8mm Tapered Wedding Band | 12480 | HRT080-10
12479 | 14k 6mm Tapered Wedding Band | 12479 | HRT060-10
12478 | 14k 5mm Tapered Wedding Band | 12478 | HRT050-10
12477 | 14k 4mm Tapered Wedding Band | 12477 | HRT040-10


Where am I going wrong?
0
Comment
Question by:purpleonyx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 35

Expert Comment

by:Raynard7
ID: 17942091
The only difference seems to be the -10 and the -6

what logic do you want to use to find each?
0
 

Author Comment

by:purpleonyx
ID: 17942110
I only need to base this on the Group ID and Item GID (which are the same number).  The mfgnum is the same as you mentioned, with the exception of the -*.  Anything after the "-" I dont even care about.  The main item number preceeds the dash.
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17942111
the above are not duplicates as the data is not identical

If you only wanted one mfgnum for each grp id you could do

Select
    x.*, (select i.mfgnum from products_items i where i.gid = x.gid limit 1)
from
(
SELECT DISTINCT DISTINCT GRP.id, GRP.description, ITM.gid
FROM products_groups AS GRP
LEFT JOIN products_items AS ITM ON GRP.id = ITM.gid
WHERE GRP.description NOT LIKE '%-%'
AND (
GRP.description LIKE '%engagement%'
OR GRP.description LIKE '%wedding%'
)
AND GRP.orderable = 'Y'
ORDER BY GRP.id DESC
) as x
order by
 x.id desc
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 17942113
please try this:

SELECT GRP.id, GRP.description, ITM.gid, ITM.mfgnum
FROM products_groups AS GRP
LEFT JOIN products_items AS ITM ON GRP.id = ITM.gid
WHERE GRP.description NOT LIKE '%-%'
AND (
GRP.description LIKE '%engagement%'
OR GRP.description LIKE '%wedding%'
)
AND GRP.orderable = 'Y'
GROUP BY GRP.id
ORDER BY GRP.id DESC
0
 
LVL 35

Assisted Solution

by:Raynard7
Raynard7 earned 250 total points
ID: 17942138
Below would remove everything before the -

SELECT DISTINCT DISTINCT GRP.id, GRP.description, ITM.gid,  SUBSTRING_INDEX(ITM.mfgnum, "-", 1) as mfgnum
FROM products_groups AS GRP
LEFT JOIN products_items AS ITM ON GRP.id = ITM.gid
WHERE GRP.description NOT LIKE '%-%'
AND (
GRP.description LIKE '%engagement%'
OR GRP.description LIKE '%wedding%'
)
AND GRP.orderable = 'Y'
ORDER BY GRP.id DESC
0
 

Author Comment

by:purpleonyx
ID: 17942837
Both examples from AngelIII and Raynard7 work perfectly!

AngelIII, thanks for reminding me of the GROUP BY command, did not even consider that!

Raynard7, your usage of the substring call is something I never even knew existed, and it seems to work the best in this situation.  Much appreciated!

Split points between the both of you.

Raynard7 -- can you explain though why we are using DISTINCT twice?

Thanks!
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17942877
typing error
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

696 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