Par*_*ngh 0 python mysql sql amazon-web-services pymssql
需要在python中查询Mysql以从具有列(table_no,is_new)的表中选择记录。table_no 是整数,is_new 是整数。所以假设我在表中有这样的数据
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
Run Code Online (Sandbox Code Playgroud)
然后我需要选择查询来获取数据,例如
table_no: 1, is_new: 1,
table_no: 2, is_new: 0,
table_no: 3, is_new: 1,
table_no: 4, is_new: 1
Run Code Online (Sandbox Code Playgroud)
换句话说,如果我有 0 和 1 in is_new 对应于 table_no 1 那么我需要 table_no: 1, is_new: 1,如果我只有 0 in is_new 对应于 table_no 1 那么我需要 table_no: 1, is_new: 0,如果我在 is_new 中只有 1 对应于 table_no 1 那么我需要 table_no: 1, is_new: 1
使用max()聚合group by
select table_no,max(is_new) as is_new
from tablename
group by table_no
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36 次 |
| 最近记录: |