dpgu5074 2015-04-07 22:22
浏览 10
已采纳

仅当某些字段值不相同时,如何插入表中?

So, for sake of simplicity, let's say I've got a table like this (f1,f2,f3 are fields):

f1   f2   f3
a    b    c
d    e    f

And say I've got a PHP array like this I want to add in to the table:

a    g    h

Now, I want to be able to add this one in, but I don't want to be able to add something like this:

a    e    f

Field f1 can have duplicates. Fields f2 & f3 cannot. I've tried adding a unique index to f2, and used REPLACE INTO, but it's not what I want. I need there to be a way to only insert if the f2 & f3 field entries are not already found on the same row in the database table.


$query = "REPLACE INTO table1 (title, description, start, end, location, affiliation)
          VALUES ('{$my_title_php_array[$count]}',
                  '{$my_description_php_array[$count]}',
                  '{$my_start_php_array[$count]}',
                  '{$my_end_php_array[$count]}',
                  '{$my_location_php_array[$count]}',
                  '{$my_affiliation_php_array[$count]}')”;

description is of data type 'TEXT', which can't be used as an INDEX (too big). Ideally, I'd like to have all be necessarily checked in comparison with the incoming PHP array entries.


Let's say I have these things in my new example's PHP array:

Title1   Desc1   1:00pm   2:00pm   Loc1   Aff1
Title1   Desc1   3:00pm   4:00pm   Loc1   Aff1
Title2   Desc2   1:00pm   2:00pm   Loc2   Aff2
Title2   Desc1   1:00pm   2:00pm   Loc3   Aff3

These should all be considered unique and entered in. I'd like to be able to enter them only when they don't exactly match a row in my database already. Desc1 is long. The descriptions are usually the most unique. Is there a way of shortening?

  • 写回答

4条回答 默认 最新

  • dongtui9168 2015-04-07 22:32
    关注

    I had to look up this information myself recently and here is the answer I found: Use multiple columns as unique identifier for mysql

    I believe it's what you're needing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮