SQL:使用系统日期为列选择数据 [英] SQL: Use system date to select data for column

查看:36
本文介绍了SQL:使用系统日期为列选择数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 2 列的表 (DBname).column1 (datatype =dates) 包含接下来 2 年的日期.column2 (datatype = integer) 我分配的一些数据.如何让 SQL 选择 column2 中与当前系统日期相对应的数据.我尝试了以下但没有成功它返回一个空白...我正在使用 MySQL

I have a table (DBname) with 2 columns. column1 (datatype =dates) has the dates for the next 2 years. column2 (datatype = integer) some data i've assigned. How can I get SQL to select the data in column2 that corresponds with the current system date. i've tried the following without success it return a blank... I am using MySQL

SELECT column2 FROM DBname WHERE column1 = CURRENT_DATE

如果我把它改成

SELECT column2 FROM DBname WHERE column1 > CURRENT_DATE

它返回column2 rowid1

如果我把它改成

SELECT column2 FROM DBname WHERE column1 > CURRENT_DATE

它返回column2 rowid3...真的很困惑...有可能是数据库本身的问题吗??

it returns column2 rowid3... really confused... is it possible the problem is in the database itself??

至于数据..我导入了一个带有column1中日期的excel文件..我写了第一个日期并对其进行了格式化然后拖下大约2年以填充其余的......至于column2数据只是我放入的数字......一个例子是

as for the data.. i imported an excel file with the dates in column1.. i wrote the first date in and formatted it then dragged down about 2 years to populate the rest.... as for the column2 data it is just numbers i have placed in... an example would be

column1 1-dec-12, 2-dec-12, 3-dec-12, 4-dec-12     
column2   2,  4, 18, 19  

推荐答案

我认为保存日期的 column1STRING.您首先需要使用 STR_TO_DATE 函数将其转换为 DATE

I think you column1 which holds date is a STRING. You need first to convert it to DATE using STR_TO_DATE function,

SELECT *
FROM table1
WHERE STR_TO_DATE(column1, '%e-%b-%y') > CURDATE()

  • SQLFiddle 演示
  • 其他来源

    这篇关于SQL:使用系统日期为列选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆