Solved

MySQL Select Distinct on Left Join Problem

Posted on 2006-11-14
7
3,513 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
  • 4
  • 2
7 Comments
 
LVL 35

Expert Comment

by:Raynard7
Comment Utility
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
Comment Utility
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
Comment Utility
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
typing error
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now