2012-05-18 31 views
0

我正在從我的players表中選擇所有內容,然後我想檢查是否在具有條件的其他表中設置了播放器id或播放器name使用其他表的條件添加結果列

我得到了兩張表 - playersshop

shop具有以下列:uid,dateline,from,done

正如我所說的,我是從players表中選擇的一切,現在我要額外列添加到名爲was_shopping的結果,這將是1playersidplayersname是在shopshop的某個地方。 dateline高於我給出的變量。

只是要確保你正確地明白了一切它應該是這樣的(它不是一個查詢,它只是一個字,例如應如何查詢作品):

SELECT * 
FROM players 
WHERE (players.id OR players.name) 
IN (
    SELECT `uid` AND `from` 
    FROM `shop` 
    WHERE `dateline` >= xxxxx 
    ) 

如果playersidplayersname將在shop之內。 uidshopfromdateline >= xxxxx它應該在結果中增加另一列:was_shopping-10(如果在shop表中發現則爲1,如果未找到,則發現0)。

如果你不明白一個詞,請發表評論。

回答

0
Select p.*, case when s.uid is null then 0 else 1 end as was_shopping 
From players as p 
left join shop as s 
where s.uid = p.id and s.dateline > = xxxxx 
0

從您的描述,我相信這將返回正確的結果。注意:對於日期選擇,我使用的值爲10:

select p.*, case when s.uid is null then 0 else case when dateline>10 then 1 else 0 end end as was_shopping 
from players p 
left join shops s 
on p.id = s.uid or p.name like concat('%',`from`,'%') 
where s.uid is not null 
+0

問題是我不能將它連接到'p.id = s.uid',因爲'p.id'完全不同於's .uid',所以它應該檢查是否有商店'uid'或'from'與玩家'id'或'name'具有相同的值。 – Cyclone

+0

什麼是id,uid,name和from的數據類型? –

+0

id是int(11),uid是int(11),name是varchar(255),from是varchar(255)。 – Cyclone

相關問題