[英]Selecting multiple entries for the same attribute being equal to another
我想修改我現有的 SQL 查詢,但無法讓它按我想要的方式工作。 (這與家庭作業/考試無關)
我想要一個獲取所有 map id、map 名稱和獎勵積分的查詢,就像現在一樣,但獲取所有不同運行 id 的地圖,而不是只獲取一次 Z1D78DC8ED51214E518B5114FE24490AE id 的地圖。 現在,如果 inf_times 中有用戶的 mapid,它將不再顯示在最終列表中。 如果 mapid 不在該用戶的 inf_times 中,我希望為每個 runid 顯示一次 map。
表結構:
inf_maps: mapid, mapname
inf_simpleranks_maps: mapid, runid, rewardpoints
inf_times: uid, mapid, runid
select a.mapid, b.mapname, a.rewardpoints
from (select r.mapid, rewardpoints
from inf_maps r, inf_simpleranks_maps srm
where r.mapid
not in (select mapid
from inf_times
where uid = %d
group by uid, mapid)
and r.mapid = srm.mapid
order by rewardpoints desc) a,
inf_maps b
where a.mapid = b.mapid;
例子
inf_times
uid mapid runid
1 4 1
inf_simpleranks_maps
mapid runid rewardpoints
4 1 10
4 2 12
inf_maps
mapid mapname
4 mapmap
查詢應該返回
mapid mapname rewardpoints
4 mapmap 12
相反,它現在什么也不返回。
嘗試以下not exists
。 這是演示。
select
im.mapid,
im.mapname,
ism.rewardpoints
from inf_maps im
join inf_simpleranks_maps ism
on im.mapid = ism.mapid
where not exists
(
select
mapid
from inf_times it
where ism.mapid = it.mapid
and ism.runid = it.runid
)
Output:
| mapid | mapname | rewardpoints |
| ----- | ------- | ------------ |
| 4 | mapmap | 12 |
你的解釋很難理解。 但是,根據您的示例數據,以下查詢將產生您期望的結果:
select m.*, r.rewardpoints
from inf_maps m
inner join inf_simpleranks_maps r on r.mapid = m.mapid
where not exists (
select 1 from inf_times t where t.mapid = m.mapid and t.runid = r.runid
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.