Skip to content
Advertisement

Need Mysql query in python 3.7 to select records from table having column (table_no, is_new)

Need Mysql query in python to select records from table having column (table_no, is_new). table_no is integer, is_new is integer. so let say if I have data like this in table

1. table_no: 1, is_new: 1
2. table_no: 1, is_new: 0
3. table_no: 2, is_new: 0
4. table_no: 2, is_new: 0
5. table_no: 3, is_new: 1
6. table_no: 4, is_new: 1
7. table_no: 4, is_new: 0

then i need select query to gete data like

table_no: 1, is_new: 1, 
table_no: 2, is_new: 0, 
table_no: 3, is_new: 1, 
table_no: 4, is_new: 1

in other words if I have 0 and 1 in is_new correspond to table_no 1 then I need table_no: 1, is_new: 1, if I have only 0 in is_new correspond to table_no 1 then I need table_no: 1, is_new: 0, if I have only 1 in is_new correspond to table_no 1 then I need table_no: 1, is_new: 1

Advertisement

Answer

use max() aggregation with group by

select table_no,max(is_new) as is_new 
from tablename
group by table_no
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement