[英]select two columns from two different tables in the same db using mysql + use the output of query in C#
[英]MySQL-extract a number from db and use it in same query
我的数据库中的table 1
有很多列,包括id
和time
。 一个 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;
如何在上述查询中包含一个子查询,该子查询提取t
并使其可由查询 A 和查询 B 访问?
我想你需要 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.