3

这是到目前为止的查询:

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`

如您所见,有很多重复。有没有办法简化这个查询,所以没有那么多事情发生?

4

1 回答 1

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
于 2012-10-21T07:41:27.647 回答