在mysql中用2行替换一行

时间:2012-12-15 20:07:20

标签: mysql sql

如何在mysql中用5行替换具有相同数据的一行。例如,我有这个表

 id   |   companyId   |      name       |  open  |  close
 1    |    1          |    mon-fri      |  10:00 |  19:00  
 2    |    1          |      sat        |  10:00 |  12:00 
 3    |    1          |      sun        |  10:00 |  12:00 
 4    |    2          |    mon-fri      |  10:00 |  16:00  
 5    |    2          |      sat        |  10:00 |  13:00 
 6    |    2          |      sun        |  10:00 |  13:00 

我想将名字字段转换为星期一,星期二,星期三,星期五,星期五。

 id   |   companyId   |      name       |  open  |  close
 1    |    1          |    mon     |  10:00 |  19:00  
 2    |    1          |    tues    |  10:00 |  19:00 
 3    |    1          |    wed     |  10:00 |  19:00 
 4    |    1          |    thur    |  10:00 |  19:00   
 5    |    1          |    fri     |  10:00 |  19:00  
 6    |    1          |    sat     |  10:00 |  12:00 
 7    |    1          |    sun     |  10:00 |  12:00 
.
.
.

1 个答案:

答案 0 :(得分:4)

您无法通过UPDATE执行此操作,但您可以做的是使用一些字符串文字的一系列INSERT INTO ... SELECT,然后使用DELETE删除行你扩大了。

/* First insert a row for each mon,tue,wed,thur,fri */
/* Column values are copied from the existing mon-fri row, and use the literal strings 'mon', 'tue', etc as the new `name` */
INSERT INTO yourtable (`companyId`, `name`, `open`, `close`)
  SELECT `companyId`, 'mon', `open`, `close` FROM yourtable WHERE `name` = 'mon-fri'
INSERT INTO yourtable (`companyId`, `name`, `open`, `close`)
  SELECT `companyId`, 'tue', `open`, `close` FROM yourtable WHERE `name` = 'mon-fri'
INSERT INTO yourtable (`companyId`, `name`, `open`, `close`)
  SELECT `companyId`, 'wed', `open`, `close` FROM yourtable WHERE `name` = 'mon-fri'
INSERT INTO yourtable (`companyId`, `name`, `open`, `close`)
  SELECT `companyId`, 'thur', `open`, `close` FROM yourtable WHERE `name` = 'mon-fri'
INSERT INTO yourtable (`companyId`, `name`, `open`, `close`)
  SELECT `companyId`, 'fri', `open`, `close` FROM yourtable WHERE `name` = 'mon-fri'

/* Then delete the original mon-fri rows which you just expanded out */
DELETE FROM yourtable WHERE `name` = 'mon-fri'

http://sqlfiddle.com/#!2/cae33/1

(信用到期:这个出色的建议是Andriy M在未经请求的编辑中的工作)

您还可以使用虚拟表减少语句数量以及表扫描次数,如下所示:

/* Cross-join the existing 'mon-fri' rows with a virtual table of day names
   of 'mon' through 'fri' and insert the resulting set back into your table */
INSERT INTO yourtable (`companyId`, `name`, `open`, `close`)
  SELECT t.`companyId`, v.`name`, t.`open`, t.`close`
  FROM yourtable t
  CROSS JOIN (
    SELECT 'mon' AS `name` UNION ALL
    SELECT 'tue' UNION ALL
    SELECT 'wed' UNION ALL
    SELECT 'thur' UNION ALL
    SELECT 'fri'
  ) v
  WHERE t.`name` = 'mon-fri';

/* Then delete the original mon-fri rows which you just expanded out */
DELETE FROM yourtable WHERE `name` = 'mon-fri';

http://sqlfiddle.com/#!2/98e35/1

评论后更新:

插入行时强制执行订单没有实际价值,最好在SELECT中完成。

要按每天companyId订购,无论采用哪种方式,都需要做一些工作。您可以使用LOWER(STR_TO_DATE())致电%a并比较工作日值,但是您还需要将thur更改为thu,因为这是MySQL缩写的方式。这导致了一堆函数调用。

相反,您可以使用CASE....中的ORDER BY为每天分配序数值,例如:

ORDER BY
 companyId,
 CASE `name`
   WHEN 'sun' THEN 1 
   WHEN 'mon' THEN 2
   WHEN 'tue' THEN 3
   WHEN 'wed' THEN 4
   WHEN 'thur' THEN 5
   WHEN 'fri' THEN 6
   WHEN 'sat' THEN 7
 ELSE 8 END

上述两种方法都不会对索引很友好。

如果您需要更高性能,我建议不要首先存储字符串'mon','tue','wed'等,而是存储其关联的工作日值。有关详细信息,请参阅DAYOFWEEK()