Question: I have a mysql table with 2 columns.

```
+---------+-----------+
| Barcode | StationID |
+---------+-----------+
|   89411 |         1 |
|   89411 |         2 |
|   89411 |         3 |
|   89412 |         1 |
|   89413 |         1 |
+---------+-----------+

```

I would like to select all valus from Barcode column which have StationID = 1 and do NOT have a StationID different than 1.

As shown in the picture Barcode 89411 appears three times with different StationID and should be excluded from the result.

Can you help me make a query?

Answer: 
Another approach is to use an `EXISTS` query:

```
SELECT t1.*
FROM yourTable t1
WHERE
    t1.StationID = 1 AND
    NOT EXISTS (SELECT 1 FROM yourTable t2
                WHERE t1.Barcode = t2.Barcode AND t2.StationID <> 1);

```

[Demo
----](https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=4e544ed735f65c3f616345820069010a)
Use aggregation function [GROUP\_CONCAT](https://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php), and use `HAVING` clause to filter out those barcodes, which has only one `StationID`, that is `'1'`: 

```
SELECT barcode, GROUP_CONCAT(DISTINCT StationID) AS stations 
FROM table_name 
GROUP BY barcode 
HAVING stations = '1';

```