Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

mysql - SQL: Use system date to select data for column

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

if i change it to

SELECT column2 FROM DBname WHERE column1 > CURRENT_DATE

it returns column2 rowid1

if i change it to

SELECT column2 FROM DBname WHERE column1 > CURRENT_DATE

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

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  
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

OTHER SOURCE


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...