我使用“codeigniter”和rownum
查询,我想在内部查询中放置WHERE
但有以下错误。怎么回事?
发生数据库错误错误号:1064
您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 靠近'WHERE'*'='0')t,(SELECT @rownum:= 0)r'在第7行
SELECT @rownum:= @ rownum + 1 rownum,t。* FROM(SELECT * FROM hotel_submits ORDER BY id desc LIMIT 0,6 WHERE * = 1)t,(SELECT @rownum:= 0)r
文件名:D:\ xampp \ htdocs \ hdr \ system \ database \ DB_driver.php
行号:330
$this->db->query("SELECT @rownum:=@rownum+1 rownum, t.*
FROM (
SELECT *
FROM hasana_you
ORDER BY id desc
LIMIT $offset, $coun_page
WHERE * = 1 //or $id instead 1
) t,
(SELECT @rownum:=0) r");
答案 0 :(得分:1)
WHERE
始终出现在LIMIT
和ORDER
之前:
每次讨论编辑
SELECT
@rownum:=@rownum+1 rownum,
t.*
FROM (
SELECT
*
FROM
hasana_you
WHERE
column_a = 1 OR
column_b = 1 OR
column_c = 1 OR
column_d = 1
ORDER BY
id desc
LIMIT
$offset, $count_page
) AS t
我在这个查询中看到了其他问题(看起来过于复杂,可能不需要子查询),但是没有你的数据库结构,我无法假定要纠正它。但是,所述关键字的顺序是主要关注点。
查看这些有关SQL语法和用法各个方面的教程文章:http://www.tizag.com/sqlTutorial/sqlwhere.php
答案 1 :(得分:1)
尝试:
$id= 1;
$f= $this->db->query("SELECT GROUP_CONCAT(column_name,
\" like '%$id%' OR \" SEPARATOR '') AS str
FROM information_schema.columns
WHERE table_name='hasana_you'");
$f1= $f->row();
$filter= substr($f1->str,0,-4);
<强>编辑:强>
$x= $this->db->query("SELECT * FROM (SELECT @rownum:=@rownum+1 rownum, t.*
FROM (SELECT @rownum:=0) r,
(SELECT *
FROM hasana_you
WHERE $filter
ORDER BY id desc
) t) x
ORDER BY id desc
LIMIT $offset, $count_page");
很难知道你想要过滤器的位置......也可以是:
$x= $this->db->query("SELECT * FROM (SELECT @rownum:=@rownum+1 rownum, t.*
FROM (SELECT @rownum:=0) r,
(SELECT *
FROM hasana_you
ORDER BY id desc
) t) x
WHERE $filter
ORDER BY id desc
LIMIT $offset, $count_page");