A
A
Alexey Bespalov2014-02-04 14:12:16
MySQL
Alexey Bespalov, 2014-02-04 14:12:16

How to make a selection based on the coincidence of one of the fields?

There is a table table of the form It is
id | field_a | field_b
necessary to make a selection on the coincidence of one field, and so that there are more than one matches.
for example

id | field_a | field_b
1  | cats   | 13
2  | cats   | 15
3  | cats   | 16
4  | dogs   | 15
5  | dogs   | 16
6  | birds  | 13
7  | birds  | 19
8  | birds  | 14

As a result, you should get
cats
dogs

This result is obtained because only these records have more than one record in field_b (15 and 16) that match.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2014-02-04
@dmtrrr

SELECT b, count(a) from test group by b having count(a) > 1;

A
Anton Alisov, 2014-02-04
@alan008

SELECT t1.field_a FROM table t1
JOIN table t2 ON (t1.field_b=t2.field_b) AND (t1.id <> t2.id)
GROUP BY t1.field_a
HAVING COUNT(t2.field_b) > 1

M
Mykola Dzedzinskyi, 2014-02-05
@dzedzinskiy

Select *
From
  (Select field_a
  From Table_1
  Where field_b in (Select field_b as bb
    From Table_1
    group by field_b
    Having count(field_a)>1)) as tmp
  Group By tmp.field_a
  Having count(field_a)>1

Execution result:
cats
dogs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question