gpt4 book ai didi

php - 避免 MySQL 中的多个列值重复

转载 作者:行者123 更新时间:2023-11-29 01:14:29 25 4
gpt4 key购买 nike

我一直发现自己编写查询以避免在有重复项时插入 - 比如

select * from foobar where bar=barbar and foo=foofoo

然后在 PHP 中使用 mysql_num_rows() 检查结果数是否 > 0,以确定是否继续我的插入。

编辑:例如,假设一个用户想要向另一个用户发送邀请。我想确保在我的邀请表中,我不会添加具有同一对 invited_idgame_id 的另一个条目。所以这需要某种检查。

这感觉效率低下(而且有点脏)。有没有更好的办法?

最佳答案

唯一索引呢?

A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. For all engines, a UNIQUE index permits multiple NULL values for columns that can contain NULL.

http://dev.mysql.com/doc/refman/5.1/en/create-table.html

编辑:

A column list of the form (col1,col2,...) creates a multiple-column index. Index values are formed by concatenating the values of the given columns.

http://dev.mysql.com/doc/refman/5.0/en/create-index.html

关于php - 避免 MySQL 中的多个列值重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8267203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com