0

I'm selecting everything from my players table, then I want to check if player id or player name is set in other table with an condition.

I got two tables - players and shop.

shop has the following columns: uid, dateline, from, done.

As I said, I'm selecting everything from players table, now I want to add additional column to the result called was_shopping and it will be the 1 when players.id or players.name is somewhere in the shop and where shop.dateline is higher than my variable given.

Just to be sure you understood everything correctly it should goes like this (its not a query, its just a word-example on how it should works in query):

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

And if players.id or players.name will be inside of the shop.uid OR shop.from with dateline >= xxxxx it should add another column to the result: was_shopping - 1 OR 0 (if was found in shop table then 1, and 0 in case if not found).

Please post a comment if you don't understand a word.

4

2 回答 2

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
于 2012-05-18T21:44:29.007 回答
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
于 2012-05-18T22:13:51.920 回答