?
Solved

MySQL Select Distinct on Left Join Problem

Posted on 2006-11-14
7
Medium Priority
?
3,526 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 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 1000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 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