gpt4 book ai didi

mysql - mysql 中某个时间段的记录和附加记录

转载 作者:行者123 更新时间:2023-11-29 19:06:19 24 4
gpt4 key购买 nike

我有一个非常简单的 MySQL 表,其中包含时间值对。

我需要获取上周(或任何时间段)的项目和一条附加记录,因为我想从数据中绘制图表,并且需要知道 1 周时间段之前的最后一个值,因此图表不会为空。 (元素非常稀有,大概每天1-2条记录)

我尝试了类似的方法,但似乎这是完全错误的语法:

SELECT * from mytable ORDER BY Time desc 
LIMIT (SELECT count(*) from mytable WHERE Time > NOW() - INTERVAL 1 WEEK)+1;

表结构:

    CREATE TABLE mytable ( 
Time datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
Value double DEFAULT NULL, PRIMARY KEY (Time) );

最佳答案

You cannot do that :

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants, with these exceptions:

  • Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.

  • Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables.

您可以做的是选择所需间隔内的行并将它们与外部的第一个行合并

SELECT  *
FROM mytable
WHERE Time > NOW() - INTERVAL 1 WEEK
UNION ALL
SELECT *
FROM mytable
WHERE Time <= NOW() - INTERVAL 1 WEEK
ORDER BY Time desc
LIMIT 1

关于mysql - mysql 中某个时间段的记录和附加记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43470558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com