dongzhong3688 2016-06-22 07:32 采纳率: 0%
浏览 14
已采纳

如果不存在,则插入帮助

Hey all i am working on a database project for a class (im just learning sql)


What i am doing successfully

1: Reading an event XML feed from another website and placing it into the database (via php)

(internal id is controlled by me and needed externalinstance_id is pulled from the feed and i want it to insert if not exists)

http://i.imgur.com/rKrRBQ8g.png


what am i trying to do?

1: The events have their own eventinstance_id (i want to use this to insert only unique) , and i Have my own Internal ID, how do i "insert if external_id is not in the database" (ignore duplicated)


what i tried so far:

1: A select where not exists in phpmyadmin(trying to build the query) however when i perform the query i get this error on phpmyadmin

*different table same idea (my testing table)

http://i.imgur.com/unkURL5.png

(i want to insert iff there isnt a internal_id of 2 for example... (same project different table same idea of what i need to do) )


Server

Win 7 (i know)

Apache 2.4.18

PHP 7.0.6

PHP EXT mysqli

phpmyadmin 4.5.1

Thanks in advanced (may or may not be a stupid question)

  • 写回答

1条回答 默认 最新

  • dpvp56187 2016-06-22 07:37
    关注

    Make eventinstance_id as Unique Id and use can use "INSERT IGNORE" QUERY. It will insert only unique values.

    Example :- INSERT IGNORE INTO table (pid,uid,data) VALUES (1,34,56); Where pid is primary key and uid is unique key.

    Below you can find more details on Insert Ignore "INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)
  • ¥50 Qt在release捕获异常并跟踪堆栈(有Demo,跑一下环境再回答)
  • ¥30 python,LLM 文本提炼
  • ¥15 关于将inet引入的相关问题
  • ¥15 关于一个倒计时的操作和显示设计
  • ¥15 提问STK的问题,哪位航天领域的同学会啊
  • ¥15 苹果系统的mac m1芯片的笔记本使用ce修改器使用不了
  • ¥15 单相逆变的电压电流双闭环中进行低通滤波PID算法改进
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 如何卸载arcgis 10.1 data reviewer for desktop