- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下表格
create table users
(
id_user int(9) AUTO_INCREMENT primary key,
email varchar(64)
);
create table friendships
(
id_friendship int(9) AUTO_INCREMENT primary key,
id_friend1 int(9),
id_friend2 int(9),
FOREIGN KEY (id_friend1) REFERENCES users(id_user),
FOREIGN KEY (id_friend2) REFERENCES users(id_user),
UNIQUE(id_friend1,id_friend2)
)
我想避免两个用户之间有两次相同的友谊。但我仍然可以像这样插入相同的友谊两次:
INSERT INTO friendships( id_friend1, id_friend2) VALUES (1,2)
INSERT INTO friendships( id_friend1, id_friend2) VALUES (2,1)
对我来说 (1,2) 和 (2,1) 是相等的,我想避免它。我怎样才能实现它?
最佳答案
您可以按特定顺序编写这些 ID。例如,在插入之前,尽量确保较小的 id 进入左列,较大的 id 进入另一列。
在你的例子中总是这样:
INSERT INTO friendships( id_friend1, id_friend2) VALUES (1,2)
永远不要这样:
INSERT INTO friendships( id_friend1, id_friend2) VALUES (2,1)
因为 id 2 比 id 1 大。
希望对您有所帮助!
关于MySQL 友谊模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33018426/
我正在尝试在 laravel 5 中建立一个友谊系统,但我被卡住了。我有一个 friends_user 表,如下所示: 编号 用户编号 friend_id 状态 重点是,我转到一个用户页面,我想看看我
很多人说我应该创建一个友谊表(userid , friendid)。 我的问题是: 每一段友谊不会有双重记录吗?喜欢: 12 是 5 的 friend + 5 是 12 的 friend 有什么办法可
我是第一次尝试使用 ORM,我定义了这样的模型: 用户模型: class User extends Eloquent { public function friends() {
您将如何塑造 friend -Grails中的友谊关系?到目前为止,我的User类有很多关注者 class User { //Searchable plugin static searchable =
我尝试在 Instagram 中建立友谊/创建/运作,但我遇到了一些错误。 账户/登录功能正常。 friendships/create 错误:“请更新您的 Instagram 应用以继续关注。” re
在本页https://dev.twitter.com/rest/reference/post/friendships/destroy你可以读到 POST 友谊/破坏是速率限制的。 但是这个其他页面ht
我是一名优秀的程序员,十分优秀!