0

I have a column with values i.e.

number   id
1        111
1        111
3        222
4        222
5        333

I'm checking if the I.D has more than one instance and if the number is the same as the second or more instance. So here, it would return 1 because there is 2x 1's with id 111, but 222 doesn't return because 3 and 4 are not the same.

How would I do such a query?

I was told that I could do two queries or more and on the second or more query I would ignore the first instance.

1
  • it's just the query i need to do, I need to return an instance where the number occurs more than once Commented Nov 9, 2012 at 4:32

3 Answers 3

2
select id, number
from the_table
group by id, number
having count(*)>1

you should have index defined on both id, and number column

ps: the order can be number, id ... depends on your composite index key

Sign up to request clarification or add additional context in comments.

2 Comments

can you please explain in detail what you mean about the index?
the index is like a shortcut key to get the matched rows. to create index: dev.mysql.com/doc/refman/5.0/en/create-index.html and further reading dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html
0

you should try using group by clause and see whether the output is what you expect:

select number,id from table_name group by number;

Comments

0

GROUP BY DOCS

Select number,id from table_name group by number;

1 Comment

Why does this not return the other values I don't understand. Don't you have to specify COUNT in this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.