我有一个MySQL查询,利用COALESCE来回溯NULL值,并为这些NULL值分配它找到的第一个NON-NULL值。
通过使用不推荐使用的函数mysql_query以及使用mysqli并调用存储过程本身从PHP脚本中解决此查询,不会影响任何行;但是,如果我手动运行查询,或者手动调用存储过程,则会正确更新行,从而生成所需的结果集。
DDL:
CREATE TABLE IF NOT EXISTS `file_index` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`file_id` int(10) unsigned NOT NULL,
`page` int(10) unsigned NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`made` datetime DEFAULT NULL,
`current` tinyint(3) unsigned DEFAULT '1',
`code` char(5) NOT NULL,
`idnumber` int(10) unsigned DEFAULT NULL,
`deconstructed` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `file_id` (`file_id`,`page`),
KEY `user_id` (`user_id`),
KEY `idnumber` (`idnumber`)
) ENGINE=MyISAM AUTO_INCREMENT=25 DEFAULT CHARSET=latin1 ROW_FORMAT=FIXED;
INSERT INTO `file_index` (`id`, `file_id`, `page`, `user_id`, `made`, `current`, `code`, `idnumber`, `deconstructed`) VALUES
(2, 637847, 2, 473, '2014-06-23 00:00:00', 1, 'INIT', 1737929, 0),
(3, 637847, 3, 473, '2014-06-23 00:00:00', 1, 'CONT', NULL, 0),
(4, 637847, 4, 473, '2014-06-23 00:00:00', 1, 'INIT', 85956, 0),
(5, 637847, 5, 473, '2014-06-23 00:00:00', 1, 'INIT', 163262, 0),
(6, 637847, 6, 473, '2014-06-23 00:00:00', 1, 'INIT', 1171457, 0),
(7, 637847, 7, 473, '2014-06-23 00:00:00', 1, 'INIT', 1173838, 0),
(8, 637847, 8, 473, '2014-06-23 00:00:00', 1, 'INIT', 2264105, 0),
(9, 637847, 9, 473, '2014-06-23 00:00:00', 1, 'INIT', 2084006, 0),
(10, 637847, 10, 473, '2014-06-23 00:00:00', 1, 'INIT', 1298597, 0),
(11, 637847, 11, 473, '2014-06-23 00:00:00', 1, 'INIT', 2263042, 0),
(12, 637847, 12, 473, '2014-06-23 00:00:00', 1, 'INIT', 1288872, 0),
(13, 637847, 13, 473, '2014-06-23 00:00:00', 1, 'CONT', NULL, 0),
(15, 637845, 2, 79, '2014-06-23 00:00:00', 1, 'INIT', 2264143, 0),
(16, 637845, 3, 79, '2014-06-23 00:00:00', 1, 'CONT', NULL, 0),
(17, 637845, 4, 79, '2014-06-23 00:00:00', 1, 'INIT', 2019138, 0),
(18, 637845, 5, 79, '2014-06-23 00:00:00', 1, 'INIT', 157726, 0),
(19, 637845, 6, 79, '2014-06-23 00:00:00', 1, 'INIT', 2087837, 0),
(22, 637845, 9, 79, '2014-06-23 00:00:00', 1, 'INIT', 2264118, 0),
(23, 637845, 10, 79, '2014-06-23 00:00:00', 1, 'INIT', 2264124, 0),
(24, 637845, 11, 79, '2014-06-23 00:00:00', 1, 'CONT', NULL, 0);
链接到SqlFiddle:http://sqlfiddle.com/#!2/6e79d
查询:
UPDATE file_index
SET idnumber = (@n := COALESCE(idnumber, @n))
ORDER BY file_id, page ASC
当前结果集:
+----------+------+----------+
| file_id | page | idnumber |
+----------+------+----------+
| 637847 | 2 | 1737929 |
| 637847 | 3 | NULL |
| 637847 | 4 | 85956 |
| 637847 | 5 | 163262 |
| 637847 | 6 | 1171457 |
| 637847 | 7 | 1173838 |
| 637847 | 8 | 2264105 |
| 637847 | 9 | 2084006 |
| 637847 | 10 | 1298597 |
| 637847 | 11 | 2263042 |
| 637847 | 12 | 1288872 |
| 637847 | 13 | NULL |
| 637845 | 2 | 2264143 |
| 637845 | 3 | NULL |
| 637845 | 4 | 2019138 |
| 637845 | 5 | 157726 |
| 637845 | 6 | 2087837 |
| 637845 | 9 | 2264118 |
| 637845 | 10 | 2264124 |
| 637845 | 11 | NULL |
+----------+------+----------+
所需的结果集:
+----------+------+----------+
| file_id | page | idnumber |
+----------+------+----------+
| 637847 | 2 | 1737929 |
| 637847 | 3 | 1737929 |
| 637847 | 4 | 85956 |
| 637847 | 5 | 163262 |
| 637847 | 6 | 1171457 |
| 637847 | 7 | 1173838 |
| 637847 | 8 | 2264105 |
| 637847 | 9 | 2084006 |
| 637847 | 10 | 1298597 |
| 637847 | 11 | 2263042 |
| 637847 | 12 | 1288872 |
| 637847 | 13 | 1288872 |
| 637845 | 2 | 2264143 |
| 637845 | 3 | 2264143 |
| 637845 | 4 | 2019138 |
| 637845 | 5 | 157726 |
| 637845 | 6 | 2087837 |
| 637845 | 9 | 2264118 |
| 637845 | 10 | 2264124 |
| 637845 | 11 | 2264124 |
+----------+------+----------+
答案 0 :(得分:0)
无论出于何种原因,我必须运行两次查询才能生效并更新行。奇怪的是,但是如果我开火两次它就会起作用。