我做了個sql語句:
Select
p.rfid, sum(d.intake) as amount
from pig_basic p left join daily_food_intake d
on p.rfid=d.rfid
group by p.rfid
order by p.rowid
where amount =0
執行到where出錯,去掉where語句則出現正確結果,請問錯在哪裡?怎麼改
這涉及where、group by 、having、order by這四個命令的先後順序,必須以這樣的順序去寫!
你的命令應該這樣寫:
注意:having 是不能用別名的~,order by 可以。
Select p.rfid, sum(d.intake) as amount
from pig_basic p
left join daily_food_intake d
on p.rfid=d.rfid
group by p.rfid
having sum(d.intake) =0
order by p.rowid