简体   繁体   English

选择日期SQL与替换月份到数字不起作用?

[英]Select date SQL with replace month to numeric not working?

I have a table containing data like this: 我有一个包含如下数据的表:

|-------|-------------------|
|   id  |       expired     |
|-------|-------------------|
|   1   |   03 Maret 2018   |
|   2   |   06 Juli 2018    |
|   3   |   02 April 2018   |
|   4   |   22 Agustus 2018 |
|   5   |   19 Juli 2018    |
|   6   |   27 Januari 2018 |
|   7   |   18 Mei 2018     |
|   8   |   13 Oktober 2018 |
|_______|___________________|

In the expired column, I use the language of INDONESIA , not English . expired列中,我使用INDONESIA的语言,而不是English

I would like to separate all content that has been expired on the website. 我想将网站上已expired所有内容分开。

If now the date (08 Maret 2018) , then there should be 2 items that have been expired, including: 如果现在是日期(08 Maret 2018) ,则应该有2个过期的项目,包括:

id = 1
id = 6

But I get different results, and the result is: 但是我得到了不同的结果,结果是:

id = 1
id = 2
id = 3

I know these unsuitable results are caused by "month" that are not assigned as numbers. 我知道这些不合适的结果是由未分配为数字的"month"引起的。 But I've changed it to SQL. 但是我将其更改为SQL。

Here's the code I use: 这是我使用的代码:

$replace_month = "replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(expired, 'Januari', '01'), 'Februari', '02'), 'Maret', '03'), 'April', '04'), 'Mei', '05'), 'Juni', '06'), 'Juli', '07'), 'Agustus', '08'), 'September', '09'), 'Oktober', '10'), 'November', '11'), 'Desember', '12')";

$dateNow = date('d m Y');

$sql = $db->query("SELECT id FROM MyTable WHERE '$dateNow' > $replace_month");

while($row = $sql->fetch_array()) {
    echo 'ID : ' . $row['id'] . '<br>';
}

The result is: 结果是:

1
2
3

Whereas I expect is: 而我的期望是:

1
6

When running SQL, the "month" in the expired column is already set as a number, for example 27 01 2018 , but why is the result still wrong? 运行SQL时,已将expired列中的"month"设置为一个数字,例如27 01 2018 ,但是结果为什么仍然错误?

The error lies in months that do not count, so it's possible to count only dates and years. 错误在于不计算在内的月份,因此可以仅计算日期和年份。

So for example now dated March 08, 2018 , then the script is only looking for the date below 08 only . 因此,例如现在日期为March 08, 2018March 08, 2018 ,那么脚本仅查找08 only以下的日期。 As for the month is ignored. 至于月份被忽略。 So that's what makes my code not working properly. 这就是使我的代码无法正常工作的原因。

Anyone can help me? 有人可以帮助我吗?

I think the query compares dates as string not date format. 我认为查询将日期比较为字符串而不是日期格式。 so how about changing to date format like this. 那么如何更改日期格式呢? I hope this code can work. 我希望这段代码可以工作。

mysql case: mysql的情况:

$sql = $db->query("SELECT id FROM MyTable WHERE 
STR_TO_DATE('$dateNow', '%d %m %Y') > STR_TO_DATE(expired, '%d %M %Y')");

oracle case: 甲骨文案:

$sql = $db->query("SELECT id FROM MyTable WHERE 
TO_DATE('$dateNow', 'DD MM YYYY') > TO_DATE(expired, 'DD MONTH YYYY')");

if expired is date format, no need to change to date format. 如果过期为日期格式,则无需更改为日期格式。 i don't know the DBMS can support the month string. 我不知道DBMS可以支持月份字符串。 if not, use STR_TO_DATE($expired, '%d %m %Y'). 如果不是,请使用STR_TO_DATE($ expired,'%d%m%Y')。

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

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