简体   繁体   English

是否可以连接到查询,但获得第二个查询之前的第一个查询结果?

[英]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 allorder 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.

相关问题 两个MySQL查询合二为一,第二个查询基于第一个查询 - Two MySQL queries in one, with the second query based on first query 将两个查询合并为一个需要第一个查询中的值的查询 - Combine the two queries into one that needs a value from second query in first 希望通过合并这些查询在一个查询中获得两个查询的结果 - Want to get results of two queries in one single query by merging these queries 如果在一个查询中第一个结果为空,如何获得 SELECT 的第二个结果? - How to get second result of SELECT if first one is null in one query? 如何在一个查询中插入多个查询以获得结果 - how to insert multiple queries in one query to get results 比较两个mysql查询的结果并在一个查询中获取结果 - Compare results of two mysql queries and get result in one query 如何将两个查询合二为一并在单个查询中获得结果? - How to join two queries in one and get results in a single query? 当第一个不可用时,mysql获得第二个可能的值 - mysql get second possible value when first one is not available 是否可以根据其中一个查询的结果将MySQL查询与多个表组合成一个查询? - Is it possible to combine MySQL queries to multiple tables into a single query based on the results from one of the queries? 我想使用 MySQL 中第二个查询中第一个查询的列将两个查询合二为一 - I want to use two queries into one using a column from first query in the second one in MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM