@Query(value="SELECT i.name\n"
"FROM item as i\n"
"INNER JOIN items_receipts as ir\n"
" ON i.id = ir.item_id\n"
"INNER JOIN receipt as r\n"
" ON ir.receipt_id = r.id\n"
"GROUP BY i.id\n"
"HAVING COUNT(r.id)=0"
,nativeQuery = true)
List<String> fiveSales();
為什么這個查詢似乎不起作用?是因為計數為空還是其他原因?
uj5u.com熱心網友回復:
您正在加入表格。因此,您將獲得所有訂購的商品。現在您查看該資料集,并為該資料集中的每個專案計算行數。問題是:您想要不在資料集中的專案。
對于查找使用[NOT] EXISTS或[NOT] IN。
select *
from item
where id not in (select item_id from items_receipts);
或者
select *
from item i
where not exists (select null from items_receipts ir where ir.item_id = i.id);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/322098.html
下一篇:從多個表插入一個表(MySql)
