[英]SQL group by missing expression
假設我有兩個表(屬性在括號中)。
從此輸出將導致出現以下問題: “顯示每次總銷售的所有產品”
我寫這個SQL查詢沒有運氣:
select pid, productname, price, count(pid) from products p, sells s having p.pid = s.sellproductid group by pid;
聽起來好像group by不能正常工作,具有sql的小知識,我不明白如何解決此問題...任何想法?
您應按表達式將所有非聚合字段包括在內。 嘗試這個:
select pid, productname, price, count(1) as ProductsSold,count(1)*price as Income
from products p, sells s
where p.pid = s.sellproductid
group by pid, productname, price
嘗試
select pid, producname,price from products p inner join sells s on p.pid=s.sellproductid group by pid.
因為函數count()返回了一行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.