简体   繁体   English

MySQL-从数据库中提取一个数字并在同一个查询中使用它

[英]MySQL-extract a number from db and use it in same query

table 1 in my db has many columns including id and time .我的数据库中的table 1有很多列,包括idtime an id is sent to the server, then I have to extract its corresponding time (let's say t ) from table and run a query like this:一个 id 被发送到服务器,然后我必须从表中提取其相应的时间(比如说t )并运行如下查询:

SELECT * FROM(
(SELECT * FROM table1 WHERE time>t AND ...clause 1...) // query A
UNION
(SELECT * FROM table1 WHERE time>t AND ...clause 2...) // query B
) h LIMIT 24;

how to include a subquery in the query mentioned above which extracts t and makes it accessible by query A and query B?如何在上述查询中包含一个子查询,该子查询提取t并使其可由查询 A 和查询 B 访问?

I think you need Mysql WITH (Common Table Expression) here,我想你需要 Mysql WITH (Common Table Expression) 这里,

WITH CTE AS (
  SELECT * FROM table1 WHERE time>t
)

SELECT * FROM (
  (SELECT * FROM CTE AND ...clause 1...) // query A
UNION
 (SELECT * FROM CTE AND ...clause 2...) // query B
) h LIMIT 24;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM