How to select multiple mysql column having more than 1 of same entry.

Hi,

I would like to request an assistant.

Let say i have the following tables :

Table Name : tblhostingconfigoptions

relid         |   configid  
------------- |   ------------
  1           |    AA  
  1           |    AA  
  2           |    C  
  3           |    DDD  
  3           |    DDD  
  3           |    DDD  
  4           |    F  
  4           |    G  
  4           |    H  
  4           |    I
 

May i know how to select only data that have more than 1 for relid having the same configid

Eg:

1. relid 1 having 2 AA configid.
2. relid 3 having 3 DDD configid
LVL 2
Shamsul KamalJunior TechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
Use this:
SELECT * FROM tblhostingconfigoptions WHERE relid='1' AND configid='AA'

Open in new window

0
Ryan ChongCommented:
if you mean to return all the duplicate records, you can try:
select a.* from tblhostingconfigoptions a
inner join
(select relid, configid
 from tblhostingconfigoptions
group by relid, configid
having  count(configid) > 1
) b
on a.relid = b.relid and b.configid = b.configid
order by a.relid, a.configid

Open in new window

use this to get a summary for you:
select relid, configid, count(configid) cnt
 from tblhostingconfigoptions
group by relid, configid
order by relid, configid

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vikas GargBusiness Intelligence DeveloperCommented:
Hi,

You can try this query

SELECT RELDI,configid FROM
(
SELECT RELDI,configid,(SELECT COUNT(configid) FROM tblhostingconfigoptions B WHERE B.RELDI = A.RELID) CT FROM tblhostingconfigoptions A
) TBL
WHERE CT > 1

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Julian HansenCommented:
This is very easy to do with the HAVING  statement

SELECT COUNT(*), relid, configid FROM tblhostingconfigoptions 
GROUP BY relid, configid
HAVING COUNT(*) > 1

Open in new window


The statement groups all relid / configid combinations together and creates a COUNT for them - it then limits the returned values to only those that have more than 1 for COUNT
0
Julian HansenCommented:
To see the HAVING solution in action you can look at this working sample here
0
Shamsul KamalJunior TechAuthor Commented:
Hi,

Thanks guys !

I use the following combination and get the results that i want :

select relid, configid, count(configid) cnt
 from tblhostingconfigoptions
group by relid, configid  HAVING COUNT(*) > 1
order by relid, configid
0
Julian HansenCommented:
@smksa,

The solution you are using does  not match the post you chose as the accepted solution
0
Shamsul KamalJunior TechAuthor Commented:
Is it ? as i choose :

select relid, configid, count(configid) cnt
 from tblhostingconfigoptions
group by relid, configid
order by relid, configid

From Ryan Chong .

And i edit the query by adding "HAVING COUNT(*) > 1" taken from your suggestion.


Please let me know if i chose wrong and wrong in distributing points.
0
Julian HansenCommented:
Please let me know if i chose wrong and wrong in distributing points.
Nope - points was fine - did not see second section of Ryan's post. 100% correct.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.