CREATE TABLE IF NOT EXISTS `experts_writings` (
`aid` bigint(12) unsigned NOT NULL AUTO_INCREMENT,
`uid` bigint(12) NOT NULL,
`title` varchar(500) NOT NULL,
`tid` int(8) NOT NULL,
`allow_share` int(2) NOT NULL DEFAULT '1',
`content` text NOT NULL,
`hits` int(8) NOT NULL DEFAULT '0',
`time` datetime NOT NULL,
`is_pic` int(2) NOT NULL DEFAULT '0',
`is_draft` int(2) NOT NULL DEFAULT '0',
PRIMARY KEY (`aid`)
);CREATE TABLE IF NOT EXISTS `experts_writings_type` (
`tid` bigint(12) unsigned NOT NULL AUTO_INCREMENT,
`uid` bigint(12) NOT NULL,
`group_id` int(2) NOT NULL DEFAULT '0',
`name` varchar(500) NOT NULL,
PRIMARY KEY (`tid`)
);很简单的一个文章表和一个文章类型表。纠结的是查询显示。分页的显示所有的数据,但显示的数据要按类型分组,而类型的顺序则是看该类型下的文章的最新时间的倒序来的。
说的不太清楚。
例:
显示为:
=======================================
类型3文章1 2010-12-28
文章2 2010-12-21
文章3 2010-11-30
文章4 2010-11-11类型1文章1 2010-12-27
文章2 2010-12-25
文章3 2010-12-24类型2文章1 2010-12-26
文章2 2010-12-23
文章3 2010-12-22
=========================================
注意每个类型下的文章的时间。这个是显示的所有的数据,并且还要分页,而不是每组只显示多少条。
如果类型3有15条数据,则第一页显示的都是类型3的数据10条,第二页显示5条,然后又才是类型1的数据。。表达能力有限啊,帮帮忙,有点纠结。
用到max也可以解决,但效率太低,给类型表加个字段“lasttime”,操作文章时,更新它,这个效率要好点,但每个文章添加,删除,修改时都要更新它,太麻烦了。
有没有更好的方法呢,最好一条语句(除了max(time)的)
`aid` bigint(12) unsigned NOT NULL AUTO_INCREMENT,
`uid` bigint(12) NOT NULL,
`title` varchar(500) NOT NULL,
`tid` int(8) NOT NULL,
`allow_share` int(2) NOT NULL DEFAULT '1',
`content` text NOT NULL,
`hits` int(8) NOT NULL DEFAULT '0',
`time` datetime NOT NULL,
`is_pic` int(2) NOT NULL DEFAULT '0',
`is_draft` int(2) NOT NULL DEFAULT '0',
PRIMARY KEY (`aid`)
);CREATE TABLE IF NOT EXISTS `experts_writings_type` (
`tid` bigint(12) unsigned NOT NULL AUTO_INCREMENT,
`uid` bigint(12) NOT NULL,
`group_id` int(2) NOT NULL DEFAULT '0',
`name` varchar(500) NOT NULL,
PRIMARY KEY (`tid`)
);很简单的一个文章表和一个文章类型表。纠结的是查询显示。分页的显示所有的数据,但显示的数据要按类型分组,而类型的顺序则是看该类型下的文章的最新时间的倒序来的。
说的不太清楚。
例:
显示为:
=======================================
类型3文章1 2010-12-28
文章2 2010-12-21
文章3 2010-11-30
文章4 2010-11-11类型1文章1 2010-12-27
文章2 2010-12-25
文章3 2010-12-24类型2文章1 2010-12-26
文章2 2010-12-23
文章3 2010-12-22
=========================================
注意每个类型下的文章的时间。这个是显示的所有的数据,并且还要分页,而不是每组只显示多少条。
如果类型3有15条数据,则第一页显示的都是类型3的数据10条,第二页显示5条,然后又才是类型1的数据。。表达能力有限啊,帮帮忙,有点纠结。
用到max也可以解决,但效率太低,给类型表加个字段“lasttime”,操作文章时,更新它,这个效率要好点,但每个文章添加,删除,修改时都要更新它,太麻烦了。
有没有更好的方法呢,最好一条语句(除了max(time)的)
参考一下这个贴子的提问方式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)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
CREATE TABLE IF NOT EXISTS `experts_writings` (
`aid` bigint(12) unsigned NOT NULL AUTO_INCREMENT,
`uid` bigint(12) NOT NULL,
`title` varchar(500) NOT NULL,
`tid` int(8) NOT NULL,
`allow_share` int(2) NOT NULL DEFAULT '1',
`content` text NOT NULL,
`hits` int(8) NOT NULL DEFAULT '0',
`time` datetime NOT NULL,
`is_pic` int(2) NOT NULL DEFAULT '0',
`is_draft` int(2) NOT NULL DEFAULT '0',
PRIMARY KEY (`aid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;--
-- 转存表中的数据 `experts_writings`
--INSERT INTO `experts_writings` (`aid`, `uid`, `title`, `tid`, `allow_share`, `content`, `hits`, `time`, `is_pic`, `is_draft`) VALUES
(1, 2161, '8888', 34, 1, '8888', 0, '2010-12-23 15:57:10', 1, 0),
(6, 2161, '7777', 34, 1, '7777', 0, '2010-12-29 10:44:13', 0, 0),
(7, 2161, '6666', 37, 1, '6666', 0, '2010-12-29 10:44:44', 1, 0),
(8, 2161, '5555', 36, 1, '5555', 0, '2010-12-29 10:45:35', 1, 0),
(9, 2161, '4444', 35, 1, '4444', 0, '2010-12-29 10:46:09', 1, 0),
(10, 2161, '3333', 34, 1, '3333', 0, '2010-12-29 10:46:50', 1, 0),
(11, 2161, 1111', 35, 1, '1111', 0, '2010-12-29 10:47:16', 1, 0),
(12, 2161, '2222', 37, 1, '2222', 0, '2010-12-29 10:47:46', 1, 0);
CREATE TABLE IF NOT EXISTS `experts_writings_type` (
`tid` bigint(12) unsigned NOT NULL AUTO_INCREMENT,
`uid` bigint(12) NOT NULL,
`group_id` int(2) NOT NULL DEFAULT '0',
`name` varchar(500) NOT NULL,
PRIMARY KEY (`tid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=43 ;--
-- 转存表中的数据 `experts_writings_type`
--INSERT INTO `experts_writings_type` (`tid`, `uid`, `group_id`, `name`) VALUES
(37, 2161, 0, 'a'),
(36, 2161, 1, 'b'),
(35, 2161, 0, 'c'),
(34, 2161, 0, 'd');
结果:+-----+------+------+----------------------+------+---------------------+--------+
| aid | uid | name | title | hits | time | is_pic |
+-----+------+------+----------------------+------+---------------------+--------+
| 12 | 2161 | a | 2222 | 0 | 2010-12-29 10:47:46 | 1 |
| 7 | 2161 | a | 6666 | 0 | 2010-12-29 10:44:44 | 1 || 11 | 2161 | c | 1111 | 0 | 2010-12-29 10:47:16 | 1 |
| 9 | 2161 | c | 4444 | 0 | 2010-12-29 10:46:09 | 1 || 10 | 2161 | d | 3333 | 0 | 2010-12-29 10:46:50 | 1 |
| 6 | 2161 | d | 7777 | 0 | 2010-12-29 10:44:13 | 0 |
| 1 | 2161 | d | 8888 | 0 | 2010-12-23 15:57:10 | 1 || 8 | 2161 | b | 5555 | 0 | 2010-12-29 10:45:35 | 1 |
+-----+------+------+----------------------+------+---------------------+--------+
| aid | uid | name| title | hits | time | is_pic |
+-----+------+------+----------------------+------+-----------------------+--------+
| 12 | 2161 | a | 2222 | 0 | 2010-12-29 10:47:46 | 1 |
| 7 | 2161 | a | 6666 | 0 | 2010-12-29 10:44:44 | 1 || 11 | 2161 | c | 1111 | 0 | 2010-12-29 10:47:16 | 1 |
| 9 | 2161 | c | 4444 | 0 | 2010-12-29 10:46:09 | 1 || 10 | 2161 | d | 3333 | 0 | 2010-12-29 10:46:50 | 1 |
| 6 | 2161 | d | 7777 | 0 | 2010-12-29 10:44:13 | 0 |
| 1 | 2161 | d | 8888 | 0 | 2010-12-23 15:57:10 | 1 || 8 | 2161 | b | 5555 | 0 | 2010-12-29 10:45:35 | 1 |
+-----+------+------+----------------------+------+-----------------------+--------+
-> from experts_writings,experts_writings_type
-> where experts_writings.tid=experts_writings_type.tid
-> order by name,experts_writings.time desc
-> ;
+-----+------+------+-------+------+--------+
| aid | uid | name | title | hits | is_pic |
+-----+------+------+-------+------+--------+
| 12 | 2161 | a | 2222 | 0 | 1 |
| 7 | 2161 | a | 6666 | 0 | 1 |
| 8 | 2161 | b | 5555 | 0 | 1 |
| 11 | 2161 | c | 1111 | 0 | 1 |
| 9 | 2161 | c | 4444 | 0 | 1 |
| 10 | 2161 | d | 3333 | 0 | 1 |
| 6 | 2161 | d | 7777 | 0 | 0 |
| 1 | 2161 | d | 8888 | 0 | 1 |
| 13 | 2161 | d | 8888 | 0 | 1 |
| 14 | 2161 | d | 8888 | 0 | 1 |
| 21 | 2161 | d | 8888 | 0 | 1 |
| 17 | 2161 | d | 8888 | 0 | 1 |
| 19 | 2161 | d | 8888 | 0 | 1 |
| 20 | 2161 | d | 8888 | 0 | 1 |
| 18 | 2161 | d | 8888 | 0 | 1 |
| 15 | 2161 | d | 8888 | 0 | 1 |
| 16 | 2161 | d | 8888 | 0 | 1 |
+-----+------+------+-------+------+--------+
17 rows in set (0.00 sec)
这样的要求?
me,experts_writings.title,experts_writings.hits,experts_writings.time,experts_wr
itings.is_pic
-> from experts_writings,experts_writings_type
-> where experts_writings.tid=experts_writings_type.tid
-> order by name,experts_writings.time desc
-> ;
+-----+------+------+-------+------+---------------------+--------+
| aid | uid | name | title | hits | time | is_pic |
+-----+------+------+-------+------+---------------------+--------+
| 12 | 2161 | a | 2222 | 0 | 2010-12-29 10:47:46 | 1 |
| 7 | 2161 | a | 6666 | 0 | 2010-12-29 10:44:44 | 1 |
| 8 | 2161 | b | 5555 | 0 | 2010-12-29 10:45:35 | 1 |
| 11 | 2161 | c | 1111 | 0 | 2010-12-29 10:47:16 | 1 |
| 9 | 2161 | c | 4444 | 0 | 2010-12-29 10:46:09 | 1 |
| 10 | 2161 | d | 3333 | 0 | 2010-12-29 10:46:50 | 1 |
| 6 | 2161 | d | 7777 | 0 | 2010-12-29 10:44:13 | 0 |
| 1 | 2161 | d | 8888 | 0 | 2010-12-23 15:57:10 | 1 |
+-----+------+------+-------+------+---------------------+--------+
8 rows in set (0.00 sec)
不就是这样么..
只是b类在a类下面了.要改?
自己感觉说得还是不够明白,要看3楼我要的结果就应该会明白,主要就是name(类型名称) 和 time(文章时间)的显示顺序。
c的2010-12-29 10:47:16
比b和d的最新时间都要大,那c应该排第二。
d排第三。b的最新时间比a,c,d的最新时间都要小,所以排第四
-> from experts_writings_type a inner join experts_writings b on a.tid=b.tid
-> inner join (select tid,max(time) as mtime from experts_writings group by tid) c on a.tid=c.tid
-> order by mtime desc,time desc,a.tid;
+-----+------+------+-------+------+---------------------+--------+
| aid | uid | name | title | hits | time | is_pic |
+-----+------+------+-------+------+---------------------+--------+
| 12 | 2161 | a | 2222 | 0 | 2010-12-29 10:47:46 | 1 |
| 7 | 2161 | a | 6666 | 0 | 2010-12-29 10:44:44 | 1 |
| 11 | 2161 | c | 1111 | 0 | 2010-12-29 10:47:16 | 1 |
| 9 | 2161 | c | 4444 | 0 | 2010-12-29 10:46:09 | 1 |
| 10 | 2161 | d | 3333 | 0 | 2010-12-29 10:46:50 | 1 |
| 6 | 2161 | d | 7777 | 0 | 2010-12-29 10:44:13 | 0 |
| 1 | 2161 | d | 8888 | 0 | 2010-12-23 15:57:10 | 1 |
| 8 | 2161 | b | 5555 | 0 | 2010-12-29 10:45:35 | 1 |
+-----+------+------+-------+------+---------------------+--------+
8 rows in set (0.00 sec)mysql>
老大,有没有不用max(time)的方法?这个max总感觉效率不怎么样啊。
我最开始想的就是这样用max的
2W多条数据
在没加什么索引之前,查询时间是0.06左右。
expalin 结果里有两个表都要检查所有数据
加了索引:
t.uidw.uid
w.tid
w.time
之后,expalin 结果里检查的数据量是降下来了,但执行时间却达到了0.2左右了。
怎么回事呢?
加索引的话,怎么加才最合适?
INNER JOIN experts_writings_type b1
ON a1.tid=b1.tid
INNER JOIN (
SELECT * FROM experts_writings a1 WHERE NOT EXISTS(SELECT 1 FROM experts_writings
WHERE a1.tid=tid AND a1.TIME<TIME) ORDER BY a1.`time` DESC,a1.tid) b2
ON a1.tid=b2.tid
ORDER BY b2.`time` DESC,nb DESC