我目前正在显示我的数据库中的最后5个事件,其中WHERE eventdate< CURDATE() 例如
CREATE TABLE venues (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
venue VARCHAR(255)
) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB;
CREATE TABLE categories (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
category VARCHAR(255)
) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB;
CREATE TABLE events (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
eventdate DATE NOT NULL,
title VARCHAR(255),
venueid INT,
categoryid INT
) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB;
INSERT INTO venues (id, venue) VALUES
(1, 'USA'),
(2, 'UK'),
(3, 'Japan');
INSERT INTO categories (id, category) VALUES
(1, 'Jazz'),
(2, 'Rock'),
(3, 'Pop');
INSERT INTO events (id, eventdate, title, venueid, categoryid) VALUES
(1,20121003,'Title number 1',1,3),
(2,20121010,'Title number 2',2,1),
(3,20121015,'Title number 3',3,2),
(4,20121020,'Title number 4',1,3),
(5,20121022,'Title number 5',2,1),
(6,20121025,'Title number 6',3,2),
(7,20121030,'Title number 7',1,3),
(8,20121130,'Title number 8',1,1),
(9,20121230,'Title number 9',1,2),
(10,20130130,'Title number 10',1,3);
SELECT DATE_FORMAT(events.eventdate,'%M %d %Y') AS DATE, title,
cats.category AS CATEGORY, loc.venue AS LOCATION
FROM events
INNER JOIN categories as cats ON events.categoryid=cats.id
INNER JOIN venues as loc ON events.venueid=loc.id
WHERE eventdate < CURDATE()
ORDER BY eventdate DESC
LIMIT 0 , 5
见下面的小提琴。
http://sqlfiddle.com/#!2/21ad85/14
我想在我的数据库中显示最后5个事件,其中eventdate&lt; (events.eventdate WHERE events.id = 10)
所以当它= 10时你应该能够看到事件id 9,8,7,6,5,其中它= 9你应该能够看到8,7,6,5,4等。
但我不太确定如何在sql中编写它。我认为它应该符合
的要求WHERE eventdate < (events.eventdate WHERE events.id =10)
但这不起作用
答案 0 :(得分:5)
也许你需要这个?
WHERE eventdate < (SELECT eventdate FROM events WHERE events.id =10)
答案 1 :(得分:0)
你能试试吗?
wHERE eventdate < curdate() and events.id < 10
updated for the typo: `events.eventdate to curdate()`