duanjiongzhen2523 2011-09-18 12:00
浏览 82
已采纳

使两列具有唯一键,但仅在两者匹配时?

I have the following table structure:

OFFER_ID -|- COUNTRY -|- URL
   1     -|-    GB   -|- http://www.example.com/1
   1     -|-    US   -|- http://www.example.com/2
   1     -|-    FR   -|- http://www.example.com/3

What I want is to update the URL when BOTH the OFFER_ID and GB are already existent within the table.

For example, if the query was:

INSERT INTO table_name (offer_id, country, url) VALUES ('1','DE', 'http://www.example.com/3')

OR

INSERT INTO table_name (offer_id, country, url) VALUES ('2','FR', 'http://www.example.com/4')

A new row would be inserted as although the values for OFFER_ID (in ex. 2) and COUNTRY (in ex. 1) are new, the values for COUNTRY (in ex. 2) and OFFER_ID (in ex. 1) aren't.

However, with a query like this:

INSERT INTO table_name (offer_id, country, url) VALUES ('1','FR', 'http://www.example.com/7')

The URL column would be updated.

I know using ON DUPLICATE KEY UPDATE url=VALUES(url) would be the way forward, but how would I be able to structure it so that ONLY when both OFFER_ID and COUNTRY are not unique, the URL column is updated as oppose to a new row being inserted?

Any help would be greatly appreciated :)!

  • 写回答

4条回答 默认 最新

  • doucao8982 2011-09-18 12:08
    关注

    You can make both a part of the primary key

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

报告相同问题?

悬赏问题

  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?