[英]Is it possible to connect to queries but get the results of first query before second one?
I want to select users from my db based on their interests and country and then i want to select them based on interests only.So here is my queries 我想根据自己的兴趣和国家/地区从数据库中选择用户,然后仅根据兴趣选择他们。所以这是我的查询
SELECT users.*
FROM users
JOIN user_opt ON users.id = user_opt.UserId
WHERE user_opt.country IN(".implode(',',$Countries).") AND user_opt.Hobbies REGEXP '".implode('|',$Interests)."' LIMIT 100
SELECT users.*
FROM users
JOIN user_opt ON users.id = user_opt.UserId WHERE user_opt.country IN(".implode(',',$Countries).") LIMIT 100
Now i want to join them into one query,but get the results of first query before second one's. 现在,我想将它们加入一个查询中,但先获取第一个查询的结果。
One probably solution can be 一种可能的解决方案是
select * from
(select 1 AS sn, users.*
...
union
select 2 AS sn, users.*
...)
order by sn;
Use union all
and order by
if you want to guarantee that the first results come before the second: 如果要保证第一个结果在第二个结果之前,请使用
union all
并order by
(SELECT u.* , 1 as which
FROM users u JOIN
user_opt uo
ON u.id = uo.UserId
WHERE u.country IN (".implode(',',$Countries).") AND
uo.Hobbies REGEXP '".implode('|',$Interests)."'
LIMIT 100
) UNION ALL
(SELECT u.*, 2 as which
FROM users u JOIN
user_opt uo
ON u.id = uo.UserId
WHERE uo.country IN(".implode(',',$Countries).")
LIMIT 100
)
ORDER BY which;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.