【问题标题】:mysql error in WHERE clauseWHERE子句中的mysql错误
【发布时间】:2015-05-15 19:07:19
【问题描述】:

很抱歉提出这样一个基本问题,但我终生无法发现这里的错误,据我所知,一切都是正确的。然而我得到了错误,也许我需要一双新鲜的眼睛来看看

您的 SQL 语法有错误;检查手册 对应于您的 MySQL 服务器版本,以便使用正确的语法 INSERT INTO expiredEvents 第 1 行的“WHERE event_id = '1243'”附近 (event_id,sport_type,比赛,回合,team1,team2,地点,event_date) 值('1243','橄榄球','Super15','3','Waratahs','Sharks','Allianz 体育场','') WHERE event_id = '1243'

 $sql="INSERT INTO expiredEvents 

(event_id,sport_type,tournament,round,team1,team2,venue,event_date) 

values ('$id','$sport','$trnmnt','$rnd','$t1','$t2','$ven','$eDate') 

WHERE event_id = '$id'"

【问题讨论】:

  • 不允许在 INSERT 语句中的位置。见here。你想做什么?
  • Where 子句只有在你做insert into select from时才允许
  • @DeepakPawar 我正在学习如果有人支持我的问题有什么问题?我是社区的新手,不需要粗鲁......谢谢
  • @gekkogurru: 你想插入新记录(插入到没有where),还是更新当前记录?
  • @AbhikChakraborty INSERT ... SELECT 中的WHERE 子句属于SELECT 子查询

标签: mysql sql


【解决方案1】:

INSERT statement 的正确语法中没有 WHERE 子句。

根据您想要实现的目标,选择以下选项之一。

插入一个新行,如果已经存在另一个具有相同event_id 的行,请不要打扰

INSERT INTO expiredEvents
    (event_id, sport_type, tournament, round, team1, team2, venue, event_date) 
VALUES
    ('$id', '$sport', '$trnmnt', '$rnd', '$t1', '$t2', '$ven', '$eDate') 

如果event_id 是表expiredEventsUNIQUE INDEX,如果已经存在另一条具有event_id = '$id' 的记录,则此查询将失败。

假设event_id 是表格的PK,请继续阅读。

插入一个新行,但前提是它不存在

INSERT IGNORE INTO expiredEvents
    (event_id, sport_type, tournament, round, team1, team2, venue, event_date) 
VALUES
    ('$id', '$sport', '$trnmnt', '$rnd', '$t1', '$t2', '$ven', '$eDate') 

IGNORE 关键字将错误转换为警告,查询成功完成,但如果另一个具有event_id = '$id' 的行已存在,则不会插入该行。

如果不存在则插入一行,如果存在则更新现有行

INSERT INTO expiredEvents
    (event_id, sport_type, tournament, round, team1, team2, venue, event_date) 
VALUES
    ('$id', '$sport', '$trnmnt', '$rnd', '$t1', '$t2', '$ven', '$eDate') 
ON DUPLICATE KEY UPDATE
    sport_type=VALUES(sport_type), round=round+1, event_date=NOW()

如果该行不存在,此查询将使用VALUES 子句中的值插入它。如果该行已经存在,那么它使用ON DUPLICATE KEY UPDATE 来知道如何更新它。此查询中修改了三个字段:

  • sport_type=VALUES(sport_type) - 使用查询中为列 sport_type 提供的值更新列 sport_type 的值(VALUES(sport_type),即 '$sport');
  • round=VALUES(round)+1 - 列 round 的值使用其当前值加 1 (round+1) 进行更新;未使用VALUES 子句中提供的值;
  • event_date=NOW() - 使用函数NOW() 返回的值修改列event_date 的值;旧值和查询的VALUES 子句中提供的值都将被忽略。

这只是一个示例,您可以在其中放置更新现有行所需的任何表达式。

用新行完全替换现有行

REPLACE INTO expiredEvents
    (event_id, sport_type, tournament, round, team1, team2, venue, event_date) 
VALUES
    ('$id', '$sport', '$trnmnt', '$rnd', '$t1', '$t2', '$ven', '$eDate') 

REPLACE statement 是对 SQL 标准的 MySQL 扩展。它首先DELETEs 具有event_id = '$id'(如果有)的行然后INSERTs 一个新行。它在功能上等同于 DELETE FROM expiredEvents WHERE event_id = '$id' 后跟此答案中上面公开的第一个查询。

【讨论】:

    【解决方案2】:

    INSERT INTO / VALUES 中不允许使用 WHERE 关键字 http://dev.mysql.com/doc/refman/5.6/en/insert.html

    详细说明您想要完成的工作。

    【讨论】:

      【解决方案3】:

      不要在插入语句中使用 where,而不仅仅是从包含确切记录的各个表中获取数据,并通过在该查询的 select 语句中添加条件来单独插入必要的行。 这是解决您的错误的简单方法,

          $sql="INSERT INTO expiredEvents 
      
      (event_id,sport_type,tournament,round,team1,team2,venue,event_date) 
      
      SELECT event_id,sport_type,tournament,round,team1,team2,venue,event_date from events WHERE event_id = '$id'"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多