dsgm5631 2012-11-23 21:28
浏览 360
已采纳

我如何确认mysql数据库中是否存在密钥列表

I have a database table containing a 'key' field which holds a system generated key.

| id | key                 | etc.
|====|=====================|================= 
|  1 | osi32i4kjwlqe       |
|----|---------------------|-----------------
|  2 | klwjeklj1343k       |
|----|---------------------|-----------------
|  3 | dfk32ld32ll23       |
|----|---------------------|----------------
|  4 | lk3jkl3j42lk4       |
--------------------------------------------

In addition, I have a list of keys:

{'osi32i4kjwlqe','ksadalala12','klwjeklj1343k','dfk32ld32ll23','lk3jkl3j42lk4','askalksl56'}

Note, that some of the keys on the list exist in the database, and others do not.

I would like, if I can, to run an efficient query that will go through the list, and affirm the existence of the key in the database.

ie: if the key exists, do nothing. if it doesn't, insert a fresh row with this key.

Does anybody know of an efficient way to do it?

Thank you.

  • 写回答

1条回答 默认 最新

  • drwf69817 2012-11-23 21:31
    关注

    I assume that your key column is declared with UNIQUE. If so, just loop and use

    INSERT IGNORE INTO table ...
    

    for each of your keys from JSON array. If not, well, make so as I think this is what you want to accomplish anyway :)

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题