M
M
Maxim2012-08-13 10:12:55
MySQL
Maxim, 2012-08-13 10:12:55

How to select records with the same value in a column?

The table has the following fields:
id, sum, member_id, date, operator_id Can

you tell me how to select all records whose date and operator_id fields are the same, have the same values?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
SadGnome, 2012-08-13
@SadGnome

For example like this. I posted it right here, I hope there are no mistakes :)

SELECT `test`.* 
FROM `test` 
LEFT JOIN 
  (SELECT `date`, `operator_id`
  FROM `test`
  GROUP BY `date`, `operator_id`
  HAVING COUNT(*)>1) as `tbl`
ON
  `test`.`date`=`tbl`.`date` AND
  `test`.`operator_id` = `tbl`.`operator_id`
WHERE `tbl`.`date` IS NOT NULL

D
demimurych, 2012-08-13
@demimurych

SELECT *
FROM `test`
WHERE date = operator_id

S
soroktu, 2012-08-13
@soroktu

here is my version

select t1.*
from test t1
where exists (select *
              from test t2
              where t2.id <> t1.id
              and t2.date = t1.date
              and t2.operator_id = t1.operator_id)

this is in case if the fields date and operator_id are necessarily filled. if they can be NULL, then you can wrap them with the COALESCE function

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question