做的一个网站 里面有信息置顶,置顶是使用 起始时间和结束时间 来判断 精确到秒
现在 同时置顶的条数是有限制的 比如说5条 超过5条就无法置顶
要提示 最早要到 Y-m-d h:i:s 才能开始置顶注 置顶是在后台操作的 手动设置起始时间 和结束时间比如 最大信息数设置为1条: 1号到4号的12点有人置顶 4号的12点到6号xx点有人置顶然后设置 5号到9号 就要提示 至少要到6号的xx点才能置顶怎么个算法 头疼死了设计程序判断部分 只需要各位文字描述
现在 同时置顶的条数是有限制的 比如说5条 超过5条就无法置顶
要提示 最早要到 Y-m-d h:i:s 才能开始置顶注 置顶是在后台操作的 手动设置起始时间 和结束时间比如 最大信息数设置为1条: 1号到4号的12点有人置顶 4号的12点到6号xx点有人置顶然后设置 5号到9号 就要提示 至少要到6号的xx点才能置顶怎么个算法 头疼死了设计程序判断部分 只需要各位文字描述
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式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)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
`infoid` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '信息ID',
`title` varchar(160) DEFAULT '' COMMENT '标题',
`topstarttime` int(10) unsigned DEFAULT NULL COMMENT '置顶开始时间',
`topendtime` int(10) unsigned DEFAULT NULL COMMENT '置顶结束时间',
PRIMARY KEY (`infoid`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;INSERT INTO `cms_info_test` VALUES ('1', '信息1', '1325389838', '1330591735');
INSERT INTO `cms_info_test` VALUES ('2', '信息2', '1330591735', '1331091735');
INSERT INTO `cms_info_test` VALUES ('3', '信息3', '1325389838', '1330691735');
INSERT INTO `cms_info_test` VALUES ('4', '信息4', '1330691735', '1330786280');
显示置顶的信息
select count(*) from cms_info_test where topstarttime >=UNIX_TIMESTAMP() and topendtime<= UNIX_TIMESTAMP()现在比如 限定 最大置顶只能为2条 然后给定条件 起始时间 1330551735 和 结束时间 1332091735
要返回一个结果 可用于判断 至少从什么时候开始 和 无法完成、置顶已满2条等
mysql> select * from `cms_info_test`;
+--------+-------+--------------+------------+
| infoid | title | topstarttime | topendtime |
+--------+-------+--------------+------------+
| 1 | 信息1 | 1325389838 | 1330591735 |
| 2 | 信息2 | 1330591735 | 1331091735 |
| 3 | 信息3 | 1325389838 | 1330691735 |
| 4 | 信息4 | 1330691735 | 1330786280 |
+--------+-------+--------------+------------+
4 rows in set (0.06 sec)mysql> select max(cnt)<2
-> from (
-> select (
-> select count(*) from cms_info_test
-> where topendtime>1330551735 and topstarttime<1332091735
-> and topendtime>a.topstarttime and topstarttime<a.topendtime
-> ) as cnt
-> from cms_info_test a
-> where topendtime>1330551735 and topstarttime<1332091735
-> ) t;
+------------+
| max(cnt)<2 |
+------------+
| 0 |
+------------+
1 row in set (0.00 sec)mysql>
这个 2012-03-18 12:10:31 怎么来的?无法理解楼主这个数据产生的逻辑。mysql> select *,FROM_UNIXTIME(topstarttime),FROM_UNIXTIME(topendtime) from `cms_info_test`;
+--------+-------+--------------+------------+-----------------------------+---------------------------+
| infoid | title | topstarttime | topendtime | FROM_UNIXTIME(topstarttime) | FROM_UNIXTIME(topendtime) |
+--------+-------+--------------+------------+-----------------------------+---------------------------+
| 1 | 信息1 | 1325389838 | 1330591735 | 2012-01-01 11:50:38 | 2012-03-01 16:48:55 |
| 2 | 信息2 | 1330591735 | 1331091735 | 2012-03-01 16:48:55 | 2012-03-07 11:42:15 |
| 3 | 信息3 | 1325389838 | 1330691735 | 2012-01-01 11:50:38 | 2012-03-02 20:35:35 |
| 4 | 信息4 | 1330691735 | 1330786280 | 2012-03-02 20:35:35 | 2012-03-03 22:51:20 |
+--------+-------+--------------+------------+-----------------------------+---------------------------+
4 rows in set (0.07 sec)
新的 测试数据CREATE TABLE `info_test` (
`infoid` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(200) DEFAULT NULL,
`starttime` datetime DEFAULT NULL,
`endtime` datetime DEFAULT NULL,
PRIMARY KEY (`infoid`)
) ENGINE=MyISAM AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;INSERT INTO `info_test` VALUES ('1', '信息1', '2012-03-01 00:00:00', '2012-03-05 23:59:59');
INSERT INTO `info_test` VALUES ('2', '信息2', '2012-03-01 00:00:00', '2012-03-03 23:59:59');
INSERT INTO `info_test` VALUES ('3', '信息3', '2012-03-01 00:00:00', '2012-03-04 23:59:59');
INSERT INTO `info_test` VALUES ('4', '信息4', '2012-03-04 00:00:00', '2012-03-06 23:59:59');
INSERT INTO `info_test` VALUES ('5', '信息5', '2012-03-05 00:00:00', '2012-03-08 23:59:59');
INSERT INTO `info_test` VALUES ('6', '信息6', '2012-03-06 00:00:00', '2012-03-07 23:59:59');
INSERT INTO `info_test` VALUES ('7', '信息7', '2012-03-07 00:00:00', '2012-03-08 23:59:59');
INSERT INTO `info_test` VALUES ('8', '信息8', '2012-03-08 00:00:00', '2012-03-10 23:59:59');
INSERT INTO `info_test` VALUES ('9', '信息9', '2012-03-09 00:00:00', '2012-03-12 23:59:59');
INSERT INTO `info_test` VALUES ('10', '信息10', '2012-03-11 00:00:00', '2012-03-14 23:59:59');
INSERT INTO `info_test` VALUES ('11', '信息11', '2012-03-15 00:00:00', '2012-03-17 23:59:59');
INSERT INTO `info_test` VALUES ('12', '信息12', '2012-03-18 00:00:00', '2012-03-19 23:59:59');
INSERT INTO `info_test` VALUES ('13', '信息13', '2012-03-20 00:00:00', '2012-03-21 23:59:59');
INSERT INTO `info_test` VALUES ('14', '信息14', '2012-03-22 00:00:00', '2012-03-23 23:59:59');
INSERT INTO `info_test` VALUES ('15', '信息15', '2012-03-24 00:00:00', '2012-03-26 23:59:59');
INSERT INTO `info_test` VALUES ('16', '信息16', '2012-03-30 00:00:00', '2012-04-02 23:59:59');
INSERT INTO `info_test` VALUES ('17', '信息17', '2012-03-30 00:00:00', '2012-04-01 23:59:59');
INSERT INTO `info_test` VALUES ('18', '信息18', '2012-04-01 00:00:00', '2012-04-02 23:59:59');
假设 最大置顶条数为3 ,2012-03-10 00:00:00 到 2011-03-13 23:59:59 应该为 1使用上述SQL语句执行结果是0 没有判断3天重叠
INSERT INTO `info_test` VALUES ('10', '信息10', '2012-03-11 00:00:00', '2012-03-14 23:59:59');这两条是否满足条件?
是否满足?
查询 2012-03-10 00:00:00 到 2011-03-13 23:59:59 。已经多次出现在这些浪费别人时间的现象了。
下面结果不是很正常吗?! 判断结果为0,也就是假,这段时间不成立。mysql> select * from info_test;
+--------+--------+---------------------+---------------------+
| infoid | title | starttime | endtime |
+--------+--------+---------------------+---------------------+
| 1 | 信息1 | 2012-03-01 00:00:00 | 2012-03-05 23:59:59 |
| 2 | 信息2 | 2012-03-01 00:00:00 | 2012-03-03 23:59:59 |
| 3 | 信息3 | 2012-03-01 00:00:00 | 2012-03-04 23:59:59 |
| 4 | 信息4 | 2012-03-04 00:00:00 | 2012-03-06 23:59:59 |
| 5 | 信息5 | 2012-03-05 00:00:00 | 2012-03-08 23:59:59 |
| 6 | 信息6 | 2012-03-06 00:00:00 | 2012-03-07 23:59:59 |
| 7 | 信息7 | 2012-03-07 00:00:00 | 2012-03-08 23:59:59 |
| 8 | 信息8 | 2012-03-08 00:00:00 | 2012-03-10 23:59:59 |
| 9 | 信息9 | 2012-03-09 00:00:00 | 2012-03-12 23:59:59 |
| 10 | 信息10 | 2012-03-11 00:00:00 | 2012-03-14 23:59:59 |
| 11 | 信息11 | 2012-03-15 00:00:00 | 2012-03-17 23:59:59 |
| 12 | 信息12 | 2012-03-18 00:00:00 | 2012-03-19 23:59:59 |
| 13 | 信息13 | 2012-03-20 00:00:00 | 2012-03-21 23:59:59 |
| 14 | 信息14 | 2012-03-22 00:00:00 | 2012-03-23 23:59:59 |
| 15 | 信息15 | 2012-03-24 00:00:00 | 2012-03-26 23:59:59 |
| 16 | 信息16 | 2012-03-30 00:00:00 | 2012-04-02 23:59:59 |
| 17 | 信息17 | 2012-03-30 00:00:00 | 2012-04-01 23:59:59 |
| 18 | 信息18 | 2012-04-01 00:00:00 | 2012-04-02 23:59:59 |
+--------+--------+---------------------+---------------------+
18 rows in set (0.00 sec)mysql> select max(cnt)<2
-> from (
-> select (
-> select count(*) from info_test
-> where endtime>'2012-03-10 00:00:00' and starttime<'2012-03-13 23:59:59'
-> and endtime>a.starttime and starttime<a.endtime
-> ) as cnt
-> from info_test a
-> where endtime>'2012-03-10 00:00:00' and starttime<'2012-03-13 23:59:59'
-> ) t;
+------------+
| max(cnt)<2 |
+------------+
| 0 |
+------------+
1 row in set (0.00 sec)