简体   繁体   中英

fetch all the unique record from table

I have an mysql query. I need fetch the all "eid" where "aid" not equal to "15" . My table is like below. The result should be in group. Example if aid != 15 then We should get only "eid" = 3 and 4 .

id | eti | aid | eid | val
1  |  2  |  15 |  1  |  WDC
2  |  2  |  11 |  1  |  USA
3  |  2  |  9  |  1  |  XYX
4  |  2  |  15 |  2  |  LDN
5  |  2  |  11 |  2  |  UK
6  |  2  |  9  |  2  |  ABC
7  |  2  |  11 |  3  |  China
8  |  2  |  9  |  3  |  HNB
9  |  2  |  18 |  4  |  China
10 |  2  |  12 |  4  |  HNB

Anybody help me? Thanks in advance...

select eid
from your_table
group by eid
having sum(aid = 15) = 0

The query below should help you:

select 'aid' from 'table'
  where 'eid' not in
    (select 'eid' from 'table'
       where 'aid'='15')
  group by 'eid';

You can use Distinct keyword. Your query will become:

 SELECT DISTINCT(eid) FROM <Your_table_name> WHERE aid != 15

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