0

我有专栏

First name | last name | nr (individual number) | pref1 (preference1) | pref2 (preference2)| pref3(preference3) | situation | distance | sex

一张表中有 100 条记录

在所有的结果中,我不能有裁员。这意味着当我在第一组结果中获得例如“2112”的单个编号(列“nr”)时,它不能显示在最后一个结果中。

SELECT DISTINCT nr FROM ap 

第一次查询的记录:

WHERE sex='F' and pref1='1' ORDER BY situation DESC, distance DESC
AND  WHERE (sex='F' and pref2='1' and situation= ' ' ) ORDER BY distance DESC
and WHERE (sex='F' and pref3='1' and situation= ' ' ) ORDER BY distance DESC
LIMIT 4

然后我想加入第二个查询的结果:

WHERE sex='M' and pref1='2' ORDER BY situation DESC, distance DESC
AND WHERE (sex='M' and pref2='2' and situation= ' ' ) ORDER BY distance DESC
AND WHERE (sex='M' and pref3='2' and situation= ' ' ) ORDER BY distance DESC
LIMIT 7

然后加入上次查询的所有记录:

WHERE sex='F' and pref1='3' ORDER BY situation DESC, distance DESC
AND WHERE (sex='F' and pref2='3' and situation= ' ' ) ORDER BY distance DESC
AND WHERE (sex='F' and pref3='3' and situation= ' ' ) ORDER BY distance DESC
LIMIT 10

有可能吗?

4

2 回答 2

0

尝试使用UNION,这只是一个使用 UNION 的简单示例

于 2012-07-07T12:03:56.603 回答
0
SELECT DISTINCT nr FROM  
(select distinct nr from ap WHERE sex='F' and pref1='1' ORDER BY situation DESC,   distance DESC Union
select distinct nr from ap WHERE (sex='F' and pref2='1' and situation= ' ' ) ORDER BY distance DESC union
select distinct nr from ap WHERE (sex='F' and pref3='1' and situation= ' ' ) ORDER BY distance DESC union
LIMIT 4)
UNION
(select distinct nr from ap WHERE sex='M' and pref1='2' ORDER BY situation DESC,   distance DESC union
select distinct nr from ap WHERE (sex='M' and pref2='2' and situation= ' ' ) ORDER BY distance DESC union
select distinct nr from ap WHERE (sex='M' and pref3='2' and situation= ' ' ) ORDER BY distance DESC union
LIMIT 7
)
UNION
(select distinct nr from ap WHERE sex='F' and pref1='3' ORDER BY situation DESC,   distance DESC union
select distinct nr from ap WHERE (sex='F' and pref2='3' and situation= ' ' ) ORDER BY distance DESC union
select distinct nr from ap WHERE (sex='F' and pref3='3' and situation= ' ' ) ORDER BY distance DESC union
LIMIT 10
))
于 2012-07-07T12:04:58.833 回答