更新数据库表列

时间:2013-11-04 12:47:45

标签: mysql sql

我想使用以下查询使用新数据更新数据库列表:

update visit set day = px
from (values (1, 'Thursday'), (2, 'Friday'), (3, 'Friday'), (4, 'Friday'),(5,'Friday'),(6,'Friday'),(7,'Saturday'),(8,'Saturday'),
(9,'Saturday'),(10,'Saturday'),(11,'Saturday'),(12,'Saturday'),(13,'Saturday'),(14,'Saturday'),(15,'Saturday'),
    (16,'Saturday'),(17,'Saturday'),(18,'Saturday'),(19,'Saturday'),(20,'Saturday'),(21,'Saturday'),(22,'Saturday'),(23,'Saturday'),(24,'Saturday'),
    (25,'Saturday'),(26,'Saturday'),(27,'Saturday'),(28,'Saturday'),(29,'Saturday'),(30,'Saturday')
    (31,'Saturday'),(32,'Saturday'),(33,'Saturday'),(34,'Saturday'),(35,'Saturday'),(36,'Saturday'),(37,'Saturday'),(38,'Saturday'),(39,'Saturday'),(40,'Saturday')
    ,(41,'Saturday'),(42,'Saturday'),(43,'Saturday'),(44,'Saturday'),(45,'Saturday')
    ,(46,'Saturday'),(47,'Saturday'),(48,'Saturday'),(49,'Saturday'),(50,'Saturday'),
    (51, 'Saturday'), (52, 'Saturday'), (53, 'Saturday'), (54, 'Saturday'),(55,'Saturday'),(56,'Saturday'),(57,'Saturday'),(58,'Saturday'),(59,'Saturday'),
    (60,'Saturday'),(61,'Saturday'),(62,'Saturday'),(63,'Saturday'),(64,'Saturday'),(65,'Saturday'),
    (66,'Saturday'),(67,'Saturday'),(68,'Saturday'),(69,'Saturday'),(70,'Saturday'),(71,'Saturday'),(72,'Saturday'),(73,'Saturday'),(74,'Saturday'),(75,'Saturday'),
    (76,'Saturday'),(77,'Saturday'),(78,'Saturday'),(79,'Saturday'),(80,'Saturday')
    (81,'Saturday'),(82,'Saturday'),(83,'Saturday'),(84,'Saturday'),(85,'Saturday'),(86,'Saturday'),(87,'Saturday'),(88,'Saturday'),(89,'Saturday'),(90,'Saturday'),(91,'Saturday'),(92,'Saturday'),(93,'Saturday'),(94,'Saturday'),(95,'Saturday')
    ,(96,'Saturday'),(97,'Saturday'),(98,'Saturday'),(99,'Saturday'),(100,'Saturday'),
    (101, 'SaturdaySaturdaySaturdaySaturday'), (102, 'Saturday'), (103, 'Saturday'), (104, 'Saturday'),(105,'SaturdaySaturday'),(106,'Saturday'),(107,'Saturday'),(108,'Saturday'),(109,'Saturday'),(110,'Saturday'),(111,'Saturday'),(112,'Saturday'),(113,'Saturday'),(114,'Saturday'),(115,'Saturday'),
    (116,'Saturday'),(117,'Saturday'),(118,'Monday'),(119,'Monday'),(120,'Tuesday'),(121,'Tuesday'),(122,'Wednesday'),(123,'Thursday'),(124,'Thursday'),(125,'Tuesday'),(126,'Thursday'),(127,'Thursday'),(128,'Thursday'),(129,'Thursday'),(130,'Thursday')
    (131,'Thursday'),(132,'Thursday'),(133,'Tuesday'),(134,'Friday'),(135,'Friday'),(136,'Friday'),(137,'Tuesday'),(138,'Wednesday'),(139,'Wednesday'),(140,'Friday'),(141,'Friday'),(142,'Monday'),(143,'Wednesday'),(144,'Wednesday')
    ,(145,'Tuesday'),(146,'Wednesday'),(147,'Tuesday''),(148,'Wednesday'),(149,'Thursday'),(150,'Friday'),(151,'Friday'),(152,'Monday'),(153,'Tuesday'),(154,'Friday'),(155,'Thursday'),(156,'Thursday'),(157,'Tuesday')
    ) p(i, px)where i = id;

当我运行查询时,出现以下错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from (values (1, 'Thursday'), (2, 'Friday'), (3, 'Friday'), (4, 'Friday'),(5,'Fr' at line 2

我一直无法弄明白,我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:4)

要测试更新脚本,请创建此表:

create table visit(day varchar(20), id int);
insert visit(day, id)
select 'Monday', 1;

更新

update visit
inner join
(
select 1 i, 'Thursday' px
union all
select 2, 'Friday'
union all
select 3, 'Friday'
union all
select 4, 'Friday'
union all
select 5,'Friday'
-- fill in the rest
) p
on p.i = visit.id
set day = p.px

确认日期值已更改:

select * from visit

结果:

DAY       ID
Thursday  1

Fiddle