这是迄今为止的查询:
select `formeid`, `name`, 'm' as `gender` from `data_pokemon`
where `genders` not like '0|_' and `formeid` not in
(select `formeid` from `dex` where `userid`=@userid and `gender`='m')
union
select `formeid`, `name`, 'f' as `gender` from `data_pokemon`
where `genders` not like '_|0' and `formeid` not in
(select `formeid` from `dex` where `userid`=@userid and `gender`='f')
union
select `formeid`, `name`, 'n' as `gender` from `data_pokemon`
where `genders`='0|0' and `formeid` not in
(select `formeid` from `dex` where `userid`=@userid and `gender`='n')
order by `formeid`
正如你所看到的,有很多重复。有没有办法简化这个查询,所以没有那么多的事情发生?
答案 0 :(得分:1)
SELECT p.formeid, p.name,
CASE
WHEN p.genders NOT LIKE '0|_' THEN 'm'
WHEN p.genders NOT LIKE '_|0' THEN 'f'
WHEN p.genders = '0|0' THEN 'n'
END AS gender
FROM data_pokemon p LEFT JOIN dex d
ON p.formeid = d.formeid
AND d.userid = @userid
AND d.gender = CASE
WHEN p.genders NOT LIKE '0|_' THEN 'm'
WHEN p.genders NOT LIKE '_|0' THEN 'f'
WHEN p.genders = '0|0' THEN 'n'
END
WHERE d.formeid IS NULL
ORDER BY p.formeid