简体   繁体   中英

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

table 1 in my db has many columns including id and time . 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:

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?

I think you need Mysql WITH (Common Table Expression) here,

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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