使用限制和where子句做分页
每个人。 请帮帮我。 现在我正在处理分页..我想根据tblcategory.cat_id每页显示5条记录。 但我的查询字符串有问题..
表tblcategory
CREATE TABLE IF NOT EXISTS tblcategory
( cat_id
int(11)NOT NULL AUTO_INCREMENT, cat_name
char(50)NOT NULL,PRIMARY KEY( cat_id
))ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 11;
表tblcontent
CREATE TABLE IF NOT EXISTS tblcontent
( con_id
int(11)NOT NULL AUTO_INCREMENT, title
char(100)NOT NULL, description
text NOT NULL, date_posted
date NOT NULL, author
char(50)NOT NULL, cat_id
int(11)NOT NULL, PRIMARY KEY( con_id
),KEY cat_id
( cat_id
))ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 78;
表tblimage
CREATE TABLE IF NOT EXISTS tblimage
( img_id
int(11)NOT NULL AUTO_INCREMENT, con_id
int(11)NOT NULL, img_name
varchar(250)NOT NULL, img_thum
varchar(250)NOT NULL,PRIMARY KEY( img_id
),KEY con_id
( con_id
))ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 9;
这是我的查询字符串,所以我的sql语句有什么问题。
SELECT * FROM tblcontent LEFT JOIN tblcategory
ON (tblcontent.cat_id = tblcategory.cat_id)
LEFT JOIN tblimage ON (tblimage.con_id = tblcontent.con_id)
ORDER BY tblcontent.date_posted DESC
LIMIT 0,5 WHERE tblcategory.cat_id=1
结果是:错误代码:1064您的SQL语法错误; 检查与您的MySQL服务器版本相对应的手册,在第5行'WHERE tblcategory.cat_id = 1'附近使用正确的语法
尝试这个
SELECT * FROM tblcontent LEFT JOIN tblcategory
ON (tblcontent.cat_id = tblcategory.cat_id)
LEFT JOIN tblimage ON (tblimage.con_id = tblcontent.con_id)
WHERE tblcategory.cat_id=1
ORDER BY tblcontent.date_posted DESC
LIMIT 0,5
首先你需要指定where条件,然后指定顺序
请参阅此处的语法
WHERE
错位 - 它需要位于ORDER BY
之前。
即
SELECT * FROM tblcontent
LEFT JOIN tblcategory ON (tblcontent.cat_id = tblcategory.cat_id)
LEFT JOIN tblimage ON (tblimage.con_id = tblcontent.con_id)
WHERE tblcategory.cat_id=1
ORDER BY tblcontent.date_posted DESC
LIMIT 0,5
链接地址: http://www.djcxy.com/p/59693.html