使用limit和where子句进行分页

时间:2013-03-19 04:02:55

标签: mysql

每个人。请帮我。现在我正在使用分页...我希望每页显示5条记录,具体取决于tblcategory.cat_id。但我的查询字符串有问题..

  1. table tblcategory

    CREATE TABLE IF NOT NOT EXISTS tblcategory(   cat_id int(11)NOT NULL AUTO_INCREMENT,   cat_name char(50)NOT NULL,   主要关键(cat_id) )ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 11;

  2. table tblcontent

    CREATE TABLE IF NOT NOT EXISTS tblcontent(   con_id int(11)NOT NULL AUTO_INCREMENT,   title char(100)NOT NULL,   description文本NOT NULL,   date_posted日期非空,   author char(50)NOT NULL,   cat_id int(11)NOT NULL,   PRIMARY KEY(con_id),   KEY cat_idcat_id) )ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 78;

  3. table tblimage

    CREATE TABLE IF NOT 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_idcon_id) )ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 9;

  4. 这是我的查询字符串,所以我的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'附近使用正确的语法

2 个答案:

答案 0 :(得分: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条件,然后指定顺序

请点击此处查看Syntax

答案 1 :(得分:0)

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