0

私は列を持っています

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

1つのテーブルapに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

次に、2番目のクエリの結果を結合したいと思います。

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 に答える