交换MySQL数据库中的记录

时间:2014-10-17 16:21:02

标签: php mysql sql

我在mysql db中有以下表格:

CREATE TABLE IF NOT EXISTS `user_defaults` (
  `user_id` int(10) unsigned NOT NULL,
  `i1` text,
  `i2` text,
  `i3` text,
  `i4` text,
  `i5` text,
  `i6` text,
  `i7` text,
  `i8` text,
  `i9` text,
  `i10` text,
  `row` int(11) NOT NULL DEFAULT '0',
  `graphkind` varchar(20) CHARACTER SET utf8 COLLATE utf8_swedish_ci NOT NULL DEFAULT '',
  `graphtitle` varchar(255) CHARACTER SET utf8 COLLATE utf8_swedish_ci NOT NULL DEFAULT 'test',
  `keys` varchar(255) CHARACTER SET utf8 COLLATE utf8_swedish_ci NOT NULL,
  `vertical` int(11) NOT NULL DEFAULT '1',
  `size` varchar(255) NOT NULL DEFAULT '',
  `more1` varchar(255) NOT NULL DEFAULT '',
  `more2` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`user_id`,`row`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;

还有一些数据:

INSERT INTO `user_defaults` (`user_id`, `i1`, `i2`, `i3`, `i4`, `i5`, `i6`, `i7`, `i8`, `i9`, `i10`, `row`, `graphkind`, `graphtitle`, `keys`, `vertical`, `size`, `more1`, `more2`) VALUES
(2, 'YEAR|Y.E. Q1F15', 'AREA|TOTAL ITALY', 'MOMENT|TOTALE MOMENTO', 'OFF PREMISE|OFF PREMISE§ON PREMISE|ON PREMISE§TOTAL|ON+OFF PREMISE', 'TOTAL|TOTAL PACKS', 'OCCASION|TOTAL OCCASION', 'BEER BY BRAND|BECK''S§BEER BY BRAND|DREHER§BEER BY BRAND|HEINEKEN§BEER BY BRAND|MORETTI§BEER BY BRAND|NASTRO AZZURRO§BEER BY BRAND|PERONI§BEER BY BRAND|TOURTEL', 'MEASURES|CONSUMERS 18+ (.000)§MEASURES|PENETRATION VS UNIVERSE§MEASURES|VOLUME (HL)', NULL, NULL, 1, 'col', 'Test', 'i1,i2,i3,i5,i6,i8,i4,i7', 1, '', '', ''),
(2, 'YEAR|Y.E. Q1F15', 'AREA|TOTAL ITALY', 'MOMENT|TOTALE MOMENTO', 'TOTAL|ON+OFF PREMISE', 'TOTAL|TOTAL PACKS', 'OCCASION|TOTAL OCCASION', 'TOTAL|TOTAL BEVERAGE', 'MEASURES|CONSUMERS 18+ (.000)', NULL, NULL, 2, 'line', 'Test', 'i1,i2,i4,i3,i5,i6,i8,i7', 1, '', '', ''),
(2, 'YEAR|Y.E. Q1F15', 'AREA|TOTAL ITALY', 'MOMENT|TOTALE MOMENTO', 'TOTAL|ON+OFF PREMISE', 'TOTAL|TOTAL PACKS', 'OCCASION|TOTAL OCCASION', 'BEER BY BRAND|TOTAL BEER BY BRAND§BEER BY BRAND|PERONI§BEER BY BRAND|MORETTI§BEER BY BRAND|NASTRO AZZURRO§BEER BY BRAND|HEINEKEN§BEER BY BRAND|BECK''S§BEER BY BRAND|DREHER§BEER BY BRAND|TOURTEL', 'MEASURES|CONSUMERS 18+ (.000)', NULL, NULL, 3, 'col', 'Pippe poppe peppe', 'i1,i2,i4,i3,i6,i5,i8,i7', 4, 's50', '', ''),
(12, 'TRIM|Q1 F13§TRIM|Q2 F13§TRIM|Q3 F13§TRIM|Q4 F13', 'AREA|TOTAL ITALY', 'BEER|BEER§BEER|PERONI', 'TOTAL|TOTAL PLACES', 'OCCASION|CONSUMPTION TOTAL OCCASION', 'MEASURE|VOLUMIZ', NULL, NULL, NULL, NULL, 1, 'col', 'Overview', 'i3,i2,i4,i5,i6,i1', 1, '', '', ''),
(13, 'TRIM|Q1 F13§TRIM|Q2 F13§TRIM|Q3 F13§TRIM|Q4 F13', 'AREA|TOTAL ITALY', 'BEER|BEER§BEER|PERONI', 'TOTAL|TOTAL PLACES', 'OCCASION|CONSUMPTION TOTAL OCCASION', 'MEASURE|VOLUMIZ', NULL, NULL, NULL, NULL, 1, 'col', 'Overview', 'i3,i2,i4,i5,i6,i1', 1, '', '', ''),
(35, 'QUARTER|Q1F15', 'AREA|TOTAL ITALY', 'MOMENT|TOTALE MOMENTO', 'OFF PREMISE|OFF PREMISE§ON PREMISE|ON PREMISE§TOTAL|ON+OFF PREMISE', 'TOTAL|TOTAL PACKS', 'OCCASION|TOTAL OCCASION', 'BEVERAGE ON+OFF|APERITIVES§BEVERAGE ON+OFF|BEER§BEVERAGE ON+OFF|CSD§BEVERAGE ON+OFF|FRUIT JUICES§BEVERAGE ON+OFF|OTHER BEVERAGE§BEVERAGE ON+OFF|PROSECCO, CHAMPAGNE§BEVERAGE ON+OFF|RTD TEA§BEVERAGE ON+OFF|SPIRITS+COCKTAIL§BEVERAGE ON+OFF|SPORT+ENERGY DRINKS§BEVERAGE ON+OFF|TOTAL BEVERAGE ON+OFF§BEVERAGE ON+OFF|WATER§BEVERAGE ON+OFF|WINE§TOTAL|TOTAL BEVERAGE', 'MEASURES|CONSUMERS 18+ (.000)§MEASURES|PENETRATION VS UNIVERSE§MEASURES|VOLUME (HL)', NULL, NULL, 2, 'line', 'Beverage - SHARE OFF STOMACH', 'i1,i2,i7,i4,i3,i6,i5,i8', 1, '', '', ''),
(35, 'QUARTER|Q1F15', 'AREA|TOTAL ITALY', 'MOMENT|TOTALE MOMENTO', 'OFF PREMISE|OFF PREMISE§ON PREMISE|ON PREMISE§TOTAL|ON+OFF PREMISE', 'TOTAL|TOTAL PACKS', 'OCCASION|TOTAL OCCASION', 'BEER BY BRAND|BECK''S§BEER BY BRAND|DREHER§BEER BY BRAND|HEINEKEN§BEER BY BRAND|MORETTI§BEER BY BRAND|NASTRO AZZURRO§BEER BY BRAND|PERONI§BEER BY BRAND|TOTAL BEER BY BRAND§BEER BY BRAND|TOURTEL', 'MEASURES|CONSUMERS 18+ (.000)§MEASURES|PENETRATION VS UNIVERSE§MEASURES|VOLUME (HL)', NULL, NULL, 3, 'line', 'Beer - SHARE OFF STOMACH', 'i1,i2,i7,i4,i3,i6,i5,i8', 1, '', '', '');

标识记录的相关字段是user_id和row,我编写了一个PHP编辑器,允许用户根据需要修改记录。显示某个用户(user_id)的记录并按"行"字段。

现在我添加了"移动" /"向下移动"按钮将两个记录交换在一起。

基本上我只需要交换"行"两个记录的字段:

UPDATE user_defaults SET row=1 WHERE user_id=xxx AND row=2
UPDATE user_defaults SET row=2 WHERE user_id=xxx AND row=1

当然这不会起作用,因为在第一行之后我会有两个记录,其中row = 1(一个错误,因为键是唯一的),而在第二行之后,它们最终都会以row = 2

我的问题:有没有办法可以同时用SQL(或PHP)交换行?可能保持查询独立于表中字段的数量和种类(user_id和row除外)?

我的意思是,如果可能的话,不是这样的:

UPDATE user_defaults SET i1=..,i2=..,i3=... WHERE user_id=xxx AND row=1
// (values from row 2)
UPDATE user_defaults SET i1=..,i2=..,i3=... WHERE user_id=xxx AND row=2
// (values from row 1)

(将一行的所有字段复制到除行之外的其他字段,但我必须在查询中写入所有字段并在每次添加字段时对其进行修改..)

0 个答案:

没有答案