tbl1有记录
id type
1 11
2 22
3 22
4 33
7 33
9 33
10 44
11 33怎样使得获得的结果的最后一个type值具有完整的数据?
比如,最后一行一定不会是2 22或4 33或7 33,因为在紧挨着这些行的后面,还有其他行与它们具有相同的type值。怎样实现这个sql?比如分两步来做:
第一步如果获取到
1 11
2 22
3 22
4 33
那么下一步就得获取到
7 33
9 33
而不能是包含别的记录,当然,能在一步中实现最好。不知我有没有把意思说清楚,谢大家了。
id type
1 11
2 22
3 22
4 33
7 33
9 33
10 44
11 33怎样使得获得的结果的最后一个type值具有完整的数据?
比如,最后一行一定不会是2 22或4 33或7 33,因为在紧挨着这些行的后面,还有其他行与它们具有相同的type值。怎样实现这个sql?比如分两步来做:
第一步如果获取到
1 11
2 22
3 22
4 33
那么下一步就得获取到
7 33
9 33
而不能是包含别的记录,当然,能在一步中实现最好。不知我有没有把意思说清楚,谢大家了。
1 11
也可能是
1 11
2 22
3 22
或
1 11
2 22
3 22
4 33
7 33
9 33
或
1 11
2 22
3 22
4 33
7 33
9 33
10 44
或
1 11
2 22
3 22
4 33
7 33
9 33
10 44
11 33
确保具有相同type值的相邻行,总是都能出现在返回结果中。
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
7 33
9 33
这个结果和前一次的结果合起来可能就是
1 11
2 22
3 22
4 33
7 33
9 33
如果能够一步实现这样的效果是最好。
SELECT MAX(a.id) AS ma FROM tth3 a ,(SELECT * FROM tth3 LIMIT 3,1) b
WHERE
EXISTS(SELECT 1 FROM tth3 c WHERE a.type<>b.type AND c.`type`<>b.type AND c.id<b.id))
select min(id),type from tbl1 group by type;
不好意思,还是应该是用max()函数,因为你要的是后面的记录。
相邻的行,且type值一样。
+----+------+
| id | type |
+----+------+
| 1 | 11 |
| 2 | 11 |
| 3 | 11 |
| 4 | 22 |
| 5 | 33 |
| 6 | 44 |
| 7 | 22 |
| 8 | 22 |
| 9 | 55 |
| 10 | 22 |
| 11 | 33 |
| 12 | 22 |
+----+------+
12 rows in set (0.00 sec)mysql> select * from test order by type,id;
+----+------+
| id | type |
+----+------+
| 1 | 11 |
| 2 | 11 |
| 3 | 11 |
| 4 | 22 |
| 7 | 22 |
| 8 | 22 |
| 10 | 22 |
| 12 | 22 |
| 5 | 33 |
| 11 | 33 |
| 6 | 44 |
| 9 | 55 |
+----+------+
12 rows in set (0.03 sec)
是你想要的东西吗
明白楼主想要什么样的东西了
不过我sql写不出,写了个过程
delimiter //
create procedure test (in count int(10))
begin
declare str varchar(50);
declare x int(5);
declare type1 int(5);
declare type2 int(5);
set x=1;
while x=1 do
set str=concat("select type into @x from test limit ",count-1,",1;");
SET @query = str;
PREPARE test FROM @query;
EXECUTE test;
DEALLOCATE PREPARE test;
set str=concat("select type into @y from test limit ",count,",1;");
SET @query = str;
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
if @x=@y then set count=count+1;
else set x=2;
end if;end while;set str=concat("select * from test limit ",count);
SET @query = str;
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;end
//
-------------------------------
mysql> select * from test;
+----+------+
| id | type |
+----+------+
| 1 | 11 |
| 2 | 11 |
| 3 | 11 |
| 4 | 22 |
| 5 | 33 |
| 6 | 44 |
| 7 | 22 |
| 8 | 22 |
| 9 | 55 |
| 10 | 22 |
| 11 | 33 |
| 12 | 22 |
+----+------+
12 rows in set (0.00 sec)mysql> call test(1);
+----+------+
| id | type |
+----+------+
| 1 | 11 |
| 2 | 11 |
| 3 | 11 |
+----+------+
3 rows in set (0.00 sec)mysql> call test(4);
+----+------+
| id | type |
+----+------+
| 1 | 11 |
| 2 | 11 |
| 3 | 11 |
| 4 | 22 |
+----+------+
4 rows in set (0.00 sec)Query OK, 0 rows affected (0.00 sec)mysql> call test(7);
+----+------+
| id | type |
+----+------+
| 1 | 11 |
| 2 | 11 |
| 3 | 11 |
| 4 | 22 |
| 5 | 33 |
| 6 | 44 |
| 7 | 22 |
| 8 | 22 |
+----+------+
8 rows in set (0.00 sec)Query OK, 0 rows affected (0.00 sec)
这个应该是楼主要的东西了吧