请帮助我如何解决这个问题…我是新手…
SELECT idt, date_played, date_end, duration FROM `v_currentshow`
其中date_play, date_end和duration字段为DATETIME
<>之前输入date_plays date_end duration1 2013-03-13 22:33:48 2013-03-13 21:58:20 00:35:282 null null 00:02:113 null null 00:00:324 null null 00:36:425 null null 00:01:306 null null 00:25:127 null null 00:01:37...n NULL NULL 00:02:32之前问题是……
- 我们如何设置下一个date_plays null值,如idt=2和另一个idt,开始与date_end值在idt=1和计数等通过整个表只是使用SELECT并使其在视图…?
- 我们如何设置下一个date_end null值,就像在idt=2和另一个idt, date_play已经设置像在问题no。1,只是使用SELECT,使其在视图上?
请帮…谢谢
的示例结果如下表所示…如果不可能……如何获得更新?
<>之前输入date_plays date_end duration1 2013-03-13 22:33:48 2013-03-13 21:58:20 00:35:282013-03-13 21:58:20 2013-03-13 22:00:31 00:02:113 2013-03-13 22:00:31 2013-03-13 22:01:03 00:00:324 2013-03-13 22:01:03 2013-03-13 22:37:45 00:36:425 2013-03-13 22:37:45 2013-03-13 22:39:15 00:01:306 2013-03-13 22:39:15 2013-03-13 23:04:37 00:25:122013-03-13 23:06:14 00:01:37...直到最后之前谢谢…
我会使用这样的解决方案,它利用MySql变量:
SELECT
idt,
date_played,
date_end,
duration
FROM (
SELECT
v_currentshow.idt,
CASE WHEN date_played IS NULL THEN @start ELSE date_played END date_played,
CASE WHEN date_end IS NULL THEN
CASE WHEN date_played IS NULL THEN @start ELSE date_played END
+ INTERVAL TIME_TO_SEC(duration) SECOND
ELSE date_end
END date_end,
@start :=
CASE WHEN date_end IS NULL THEN
CASE WHEN date_played IS NULL THEN @start ELSE date_played END
+ INTERVAL TIME_TO_SEC(duration) SECOND
ELSE date_end
END,
duration
FROM
v_currentshow,
(SELECT @start := null) r
) s
请参阅此处。如果您确定没有空格,并且除了第一行以外的每一行都有空值,则可以进一步简化此查询。
你应该用PHP来处理,如果这个查询听起来很疯狂,你可能最好用PHP来后处理。
我的解决方案似乎有效,因为你用PHP
标记了你的问题。
承认你的查询返回一个数组,你可以这样做:
$results = array(); // the array of results returned by the query
$previousEndDate = null; // default it to null to avoid weird situations
foreach ($results as &$row) { // you can replace the pointer (&) with a new array that gets the proper values injected into it ~ but it's more readable because it highlights the most important lines
if ($row['date_played'] === null && $previousEndDate !== null) {
$row['date_played'] = $previousEndDate;
}
if ($row['date_end'] === null) {
$dateInterval = new DateInterval('P0000-00-00T'. $row['duration']); // we create a valid PHP interval to add on top of the date_played
$datePlayed = new DateTime($row['date_played']); // create a valid DateTime object off the SQL value
$row['date_end'] = $datePlayed->add($dateInterval); // and we add the interval to the datePlayed
}
$previousEndDate = $row['date_end'];
}
关于DateTime对象和DateInterval对象的更多文档。作为奖励,上面的代码可以支持时区。
你可以把这些值保存回数据库;我不会使用一个查询解决方案,因为它可能会花费更多的时间,并显著降低数据库的速度。