Mysql 如何从一个表连接两个长查询?
我有专栏Mysql 如何从一个表连接两个长查询?,mysql,join,Mysql,Join,我有专栏 First name | last name | nr (individual number) | pref1 (preference1) | pref2 (preference2)| pref3(preference3) | situation | distance | sex 一个表中有100条记录 从结果来看,我不能裁员。这意味着当我在第一组结果中得到前一个示例“2112”的单个数字(列“nr”)时,它不能显示在最后一个结果中 SELECT DISTINCT nr FROM a
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
有可能吗?尝试使用,这只是使用UNION的一个简单示例,因为您演示的SQL无效,不清楚您想要什么。您能展示一些示例表行和查询输出的示例吗?关系演算不能确保这一点,但在MYSQL/ORACLE中,您所说的可能是正确的。这与有效的SQL不同。多个
WHERE
和多个ORDER BY
每个工会小组?不起作用…它不适用于SQL:(还有其他想法吗?我从昨天就开始尝试了
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
))