[英]Getting the Correct SQL Query Syntax for MariaDB
I am converting some SQL queries to run against MariaDB instead of via SQL Anywhere. 我正在将一些SQL查询转换为针对MariaDB运行,而不是通过SQL Anywhere运行。 One part of the query I need to update looks like this:
我需要更新的查询的一部分如下所示:
and datepart(mm,c.dob) = datepart(mm,today()) + 1
After consulting some MariaDB documentation, I think the second part may need to be this: 在查阅了一些MariaDB文档之后,我认为第二部分可能需要这样:
MONTH(NOW()) + 1
But, I'm not sure how to re-create the logic from the original line, since datepart
is not used in MariaDB. 但是,我不确定如何从原始行重新创建逻辑,因为MariaDB中未使用
datepart
。 When I try this, the query runs: 当我尝试这样做时,查询运行:
and MONTH(c.dob) = MONTH(NOW()) + 1
Does this look like the correct replacement syntax? 这看起来像正确的替换语法吗?
Hiding a column inside a function call prevents use of an index. 将列隐藏在函数调用中可防止使用索引。 Try to avoid such.
尽量避免这种情况。
When doing date arithmetic, use syntax like this: 在进行日期算术时,请使用如下语法:
date_value + INTERVAL 4 DAY
If using MONTH(NOW())
, what should you expect in December? 如果使用
MONTH(NOW())
,那么您在12月应该期待什么?
You can treat dates as strings in the format 2019-09-06
, so this is one way to get the start of the current month: 您可以将日期视为字符串,格式为
2019-09-06
,因此这是获取本月开始日期的一种方法:
mysql> SELECT NOW(), CONCAT(LEFT(CURDATE(), 7), '-01');
+---------------------+-----------------------------------+
| NOW() | CONCAT(LEFT(CURDATE(), 7), '-01') |
+---------------------+-----------------------------------+
| 2019-09-06 11:38:37 | 2019-09-01 |
+---------------------+-----------------------------------+
Hence this will give you the start of next month, even in December: 因此,这将为您提供下个月的开始,甚至是十二月:
CONCAT(LEFT(CURDATE(), 7), '-01') + INTERVAL 1 MONTH
It is usually preferable to compare dates than to pick apart a date -- I am thinking of the December issue. 比较日期通常比分隔日期更可取-我想到的是12月的问题。 And leap-day.
和leap日。
WHERE my_date >= CONCAT(LEFT(CURDATE(), 7), '-01') + INTERVAL 1 MONTH
will check for my_date
being next month (or later). 将检查
my_date
是下个月(或更晚)。 This works whether my_date
is DATE
, DATETIME
, or TIMESTAMP
. 无论
my_date
是DATE
, DATETIME
还是TIMESTAMP
。
But, since dob
is "date of birth", most of what I say is irrelevant, and you really need MONTH(dob)
但是,由于
dob
是“出生日期”,因此我所说的大部分内容都不相关,您确实需要MONTH(dob)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.