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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?