手册的comment里的一个例子,间接达到了从句用limit的功能 但是如果你的mysql连这个也不支持的话,可能只有分两步走了http://dev.mysql.com/doc/mysql/en/row-subqueries.htmlIf you try the following you will find it fails in mysql version 4.1, SELECT COUNT(*) FROM TABLE_1 WHERE ROW (PK_PART_1, PK_PART_2) IN ( SELECT PK_PART_1, PK_PART_2 FROM TABLE_2 LIMIT 10 -- This is the basis of the whole idea ) AND Whatever;The above fails with the warning ... ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'However, you can get the desired behaviour with a slight rewrite...SELECT COUNT(*) FROM TABLE_1 INNER JOIN ( SELECT PK_PART_1, PK_PART_2 FROM TABLE_2 LIMIT 10 -- HEY HEY! ) AS VIRTUAL_TABLE_2 ON TABLE_1.PK_PART_1 = TABLE_2.PK_PART_1 AND TABLE_1.PK_PART_2 = TABLE_2.PK_PART_2 WHERE Whatever;And it works like a charm!
1、读取倒数第10条记录的id create temporary table temp select id from topic order by id desc limit 10,1 2、在后0条记录中查找 select topic.id,postTime from topic,temp where topic.id>=temp.id and parentID=123 order by topic.id desc limit 10
呵呵,瞎说一下哈: 为什么要2次查询呢。呵呵,把where后面的条件放到php中判断就是了3:) select id,postTime,parentID from topic where 1 order by id desc limit 10; while(){ if(parentID!=123)continue; else... }
回复人: xuzuning(唠叨) ( ) 信誉:679 ====== 我也是这样想的,在本地用root帐号试了一下,完全可以,但是用我的虚拟主机的帐号不可以,查了一下手册,手册上说4.0.2版本以后都要对创建临时表权限进行单独设置,看来我没有创建临时表的权限,但是我可以创建表 create table temp select id from topic order by id desc limit 10,1 select topic.id,postTime from topic,temp where topic.id>=temp.id and parentID=123 order by topic.id desc limit 10 drop table temp; 不知道创建临时表是不是比直接创建表要快很多因为这个程序是要发布的,所以我想找一种通用的方法,照顾一下低版本用户。回复人: helloyou0(你好!) ( ) 信誉:100 ============== 我这个版本的不支持子查询。
但是如果你的mysql连这个也不支持的话,可能只有分两步走了http://dev.mysql.com/doc/mysql/en/row-subqueries.htmlIf you try the following you will find it fails in mysql version 4.1, SELECT
COUNT(*)
FROM
TABLE_1
WHERE
ROW (PK_PART_1, PK_PART_2) IN
(
SELECT
PK_PART_1, PK_PART_2
FROM
TABLE_2
LIMIT
10 -- This is the basis of the whole idea
)
AND
Whatever;The above fails with the warning ... ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'However, you can get the desired behaviour with a slight rewrite...SELECT
COUNT(*)
FROM
TABLE_1
INNER JOIN
(
SELECT
PK_PART_1, PK_PART_2
FROM
TABLE_2
LIMIT
10 -- HEY HEY!
) AS VIRTUAL_TABLE_2
ON
TABLE_1.PK_PART_1 = TABLE_2.PK_PART_1 AND
TABLE_1.PK_PART_2 = TABLE_2.PK_PART_2
WHERE
Whatever;And it works like a charm!
create temporary table temp select id from topic order by id desc limit 10,1
2、在后0条记录中查找
select topic.id,postTime from topic,temp where topic.id>=temp.id and parentID=123 order by topic.id desc limit 10
为什么要2次查询呢。呵呵,把where后面的条件放到php中判断就是了3:)
select id,postTime,parentID from topic where 1 order by id desc limit 10;
while(){
if(parentID!=123)continue;
else...
}
======
我也是这样想的,在本地用root帐号试了一下,完全可以,但是用我的虚拟主机的帐号不可以,查了一下手册,手册上说4.0.2版本以后都要对创建临时表权限进行单独设置,看来我没有创建临时表的权限,但是我可以创建表
create table temp select id from topic order by id desc limit 10,1
select topic.id,postTime from topic,temp where topic.id>=temp.id and parentID=123 order by topic.id desc limit 10
drop table temp;
不知道创建临时表是不是比直接创建表要快很多因为这个程序是要发布的,所以我想找一种通用的方法,照顾一下低版本用户。回复人: helloyou0(你好!) ( ) 信誉:100
==============
我这个版本的不支持子查询。