我试图编写一个查询:
我有这个:
(SELECT *, 1 as SORY_QUERY1 FROM xbamZ where state = 'Minnesota' and industry = 'Miscellaneous' and id != '229' limit 4)
UNION
(SELECT *, 2 FROM xbamZ where state = 'Minnesota' limit 2)
UNION
(SELECT *, 3 FROM xbamZ where industry = 'Miscellaneous' limit 1)
我是如何(或是?)这样做的?我接近了吗?这个查询给了我重复
答案 0 :(得分:2)
我认为不需要工会和三个select
。一个人也会工作
SELECT a.*
FROM
(
SELECT xbamZ.*,
CASE
WHEN state = 'Minnesota' and industry = 'Miscellaneous' and id != '229' THEN 1
WHEN state = 'Minnesota' THEN 2
WHEN industry = 'Miscellaneous' THEN 3
END as rnk
FROM xbamZ
where state = 'Minnesota' or industry = 'Miscellaneous'
)a
ORDER BY rnk
LIMIT 4;