Solved

MySQL Select Distinct on Left Join Problem

Posted on 2006-11-14
7
3,519 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
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
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
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

22 Experts available now in Live!

Get 1:1 Help Now