Comparing columns mysql

I want to compare columns in a table.

I have something like this:

id    n_emp    paid
1     1        0
2     1        0
3     1        0
4     2        1
5     2        0
6     2        0
7     3        1
8     3        1

And I want this result:

n_emp    paid
1        0
2        0
3        1

In other words, if all paid it should return 1, if all not paid it should return 0.

Can anybody help, please?

Upvotes: 0

Views: 62

Answers (2)

Praveen Prasannan
Praveen Prasannan

Reputation: 7123

select `n_emp`, min(`paid`)
from Table1 Group by n_emp;

Fiddle

Upvotes: 0

Klas Lindbäck
Klas Lindbäck

Reputation: 33273

MIN(paid) will be 0 if one or more rows have 0.

SELECT n_emp, MIN(paid) FROM myTable GROUP BY n_emp

Upvotes: 2

Related Questions