简体   繁体   中英

How to fetch those rows where all values are same in mysql

I have a table:

Unique Value        Remarks
URTA-144            ACC
URTA-144            ADD
URTA-144            ADT
URTA-145            ALL
URTA-145            ALL
URTA-145            ALL

I want to fetch those rows where for each distinct Unique Value I have same value in Remarks .

For Example:

Unique Value
URTA-145

Try this:

create table Tbl(UniqueValue char(8), Remarks char(3));
insert into Tbl values
('URTA-144', 'ACC'),
('URTA-144', 'ADD'),
('URTA-144', 'ADT'),
('URTA-145', 'ALL'),
('URTA-145', 'ALL'),
('URTA-145', 'ALL');

select UniqueValue from Tbl
group by UniqueValue
having count(distinct Remarks) = 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM