douxiji8707 2013-04-17 11:40
浏览 91

如何检查mysql表中多行中与每行中唯一数据关联的值

I'm developing a record management application using php. I've designed a table named records with fields id, recordname, recordvalue, recordid. For example if a record consists of 3 data firstname, lastname, gender. 3 data (John Doe Male, Jane Doe Female and Sherlcok Holmes Male) is inserted in the table as below-

----+-------------+--------------+-----------
id  | recordname  | recordvalue  | recordid
----+-------------+--------------+-----------
1   | firstname   | John         | 001
2   | lastname    | Doe          | 001
3   | gender      | Male         | 001
4   | firstname   | Jane         | 002  
5   | lastname    | Doe          | 002
6   | gender      | Female       | 002
7   | firstname   | Sherlock     | 003
8   | lastname    | Holmes       | 003
9   | gender      | Male         | 003

----+-------------+--------------+-----------

my question is when i'm going to insert a new record in the table i need to check if there is any duplication of the record. That means any field for different records can be duplicated anytime but all the fields having same values for a record can't be duplicated. From previous example records having value John Doe Female, Jane Doe Male, Sherlock Doe Male etc are OK but again inserting John Doe Male is not expected and it'll give an error.

How can i achieve this easily with PHP?

Thanks in advance.

  • 写回答

7条回答 默认 最新

  • doushansu9012 2013-04-17 11:54
    关注

    my question is when i'm going to insert a new record in the table i need to check if there is any duplication of the record.

    Your “records” are something different than a normal database record (= one row) here.

    That means any field for different records can be duplicated anytime but all the fields having same values for a record can't be duplicated. From previous example records having value John Doe Female, Jane Doe Male, Sherlock Doe Male etc are OK but again inserting John Doe Male is not expected and it'll give an error.

    So the combination of several records has to be unique in your case.

    How can i achieve this easily with PHP?

    You could SELECT first and see if you get a result for the data to be inserted already, as Yadav suggested in his answer. But that will result in a possible TOCTTOU problem – to avoid that, you will have to encapsulate SELECT and INSERT into a transaction.

    The other way would be to use an INSERT … SELECT statement. The SELECT statement must be formulated in a way that it only returns values to insert if it does not find them already – counting the matching records and using HAVING to filter for only those that give the right number of records (three in your example) would do that.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭