2

I seem to be missing something fundamental. I an a new refugee from an old esoteric French database system (4D) and Im new to mySQL.

Given The following DB:

CREATE  TABLE `stuff` (
  `ID` INT NOT NULL AUTO_INCREMENT ,
  `Product` VARCHAR(45) NULL ,
  `Sell_by` DATE NULL ,
  PRIMARY KEY (`ID`) );


INSERT INTO `Stuff` (`Product`, `Sell_by`) VALUES ('Milk', '2013-05-16');
INSERT INTO `Stuff` (`Product`, `Sell_by`) VALUES ('Cheese', '2013-06-15');
INSERT INTO `Stuff` (`Product`, `Sell_by`) VALUES ('Yogurt', '2013-07-02');
INSERT INTO `Stuff` (`Product`, `Sell_by`) VALUES ('Bread', '2013-08-17');
INSERT INTO `Stuff` (`Product`, `Sell_by`) VALUES ('Twinkies', '2099-04-16');

http://sqlfiddle.com/#!2/3ef48e/1

Why doesn't this return Milk Cheese and Yogurt? It returns nothing.

SELECT * FROM  Stuff Where Sell_by <= 2013-07-04;

But this returns everything?

SELECT * FROM  Stuff Where Sell_by >= 2013-07-04;
4

3 回答 3

7

Your query is not actually checking for a date, but a numeric expression ( 2013 - 7 - 4 ).

Put the date expression in single quotes ('2013-07-14') and you should be fine.

于 2013-07-02T19:31:40.490 回答
5

I think you are missing quotes; try this:

SELECT * FROM  Stuff Where Sell_by <= '2013-07-04';
于 2013-07-02T19:32:19.907 回答
3

Use quotes around date like:

SELECT * FROM  Stuff Where Sell_by <= '2013-07-04';
于 2013-07-02T19:32:43.573 回答