我有这个原始的SQL查询:
select tablename.id, tablename.prodId, tablename.status
from tablename inner join (
select max(tablename.id) id, tablename.prodid
from tablename t
where not exists (
select 1 from tablename
where prodid = t.prodid and status in ('Success', 'Fail')
)
group by tablename.prodid
) g on g.id = t.id
如何用Sqlalchemy Core(不是ORM)语法编写?
我使用了Postgresql。 我对此声明有疑问:
select max(t.id) id, t.serial_id
from ppod_register t
where not exists (
select 1 from ppod_register
where serial_id = t.serial_id and register_status in ('SUCCESS', 'ERROR_DEVICE')
)
我尝试使用sqlalechemy:
_exists = sql.select([1]) \
.select_from(ppod_register) \
.where(ppod_register.c.register_status.in_((PpodRegisterStatus.SUCCESS, PpodRegisterStatus.ERROR_DEVICE))) \
.where(serial_id_p == ppod_register.c.serial_id)
from sqlalchemy import exists
_exists = exists(_exists)
query = sql.select([serial_id_p]).where(~_exists)
但是那当然行不通。 我该怎么做这样的声明:
where serial_id = t.serial_id and register_status in ('SUCCESS', 'ERROR_DEVICE')