【问题标题】:1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version1064 (42000): 你的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册
【发布时间】:2019-05-27 13:27:34
【问题描述】:
sql = "INSERT IGNORE INTO record1 (s_id,s_date,s_in_time,s_p) VALUES (%s,%s,%s,%s)"
       SELECT s_id, s_date, s_in_time, s_out_time, s_p 
       FROM staff_attendance 
       WHERE staff_attendance.s_id 
         AND staff_attendance.s_date != record.s_id 
         AND record.s_date 

我正在使用此查询来填充我的数据,我只希望输入数据库的条目是唯一的

【问题讨论】:

  • 这是什么语言? C?爪哇?你能显示完整的代码吗?你如何替换%s
  • 样本数据和期望的结果会有所帮助,对您要实现的逻辑的解释也会有所帮助。您的查询毫无意义。
  • 在目前的形式中,这些行在语法上不可能是正确的。在第一行中,您将 SQL 语句放入变量中,但没有出现对 %s 占位符的替换。之后会出现一个 SELECT 语句,但没有预编译器指令,也没有变量或其他构造来处理它。

标签: sql mariadb


【解决方案1】:

首先,values 语句不是必需的。

其次,你有一个表record的引用,它不存在。

我怀疑您不希望在 s_id/s_date 上重复(尽管这是一个猜测)。您需要从任何您想要的唯一索引/约束开始。所以,从那个开始(如果它不存在):

create index unq_record1_s_id_s_date on record1(s_id, s_date);

那么,我会推荐on duplicate key update 而不是insert ignore。这允许语句捕获其他类型的错误:

insert into record1 (s_id, s_date, s_in_time, s_out_time, s_p)
    select sa.s_id, sa.s_date, sa.s_in_time, sa.s_out_time, sa.s_p 
    from staff_attendance sa
    on duplicate key update s_id = values(s_id);

【讨论】:

    【解决方案2】:

    如果你想要插入选择,你应该使用

      INSERT IGNORE INTO record1 (s_id,s_date,s_in_time,s_p) 
      SELECT s_id, s_date, s_in_time,  s_p 
      FROM staff_attendance 
      WHERE staff_attendance.s_id 
         AND staff_attendance.s_date != record.s_id 
          AND record.s_date 
    

    【讨论】:

      猜你喜欢
      • 2018-11-09
      • 2018-11-28
      • 2021-12-03
      • 2017-06-30
      • 2019-11-22
      • 2017-11-22
      • 2019-01-11
      • 1970-01-01
      • 2014-03-14
      相关资源
      最近更新 更多