连接同一个表中的两个外键引用同一个主键
Posted
技术标签:
【中文标题】连接同一个表中的两个外键引用同一个主键【英文标题】:Joining two foregin keys in the same table references the same primary key 【发布时间】:2019-12-23 12:12:34 【问题描述】:我正在尝试将多个表(BooksReview
、Followers
、Books
)与Users
表连接起来,其中Books
引用userId
来自Users
、BooksReview
引用userId
这是userId
审查了一本书和参考bookId
来自Books
这是经过审查的书ID,最后但并非最不重要的是Followers
表(这里我认为问题出在哪里)有两个来自同一主键的引用,它具有来自Users
表中的userId
的follower
和followed
也来自Users
表中的userId
。
问题:我创建了一个 mysql 查询来获取基于特定用户的特定书籍的书评数量,并获取该用户的书籍数量和他/她拥有的关注者数量,但是当我添加Followers
加入我的查询部分,它显示所有值的结果为 0,预期值为 4 本书、4 条评论和 1 个关注者。
我尝试更改查询中的连接类型,但结果相同,并在同一个表中搜索连接两个外键以获得相同的主键,但我没有发现任何有用的东西。
```
CREATE TABLE IF NOT EXISTS `Authors`.`Users` (
`userId` VARCHAR(100) NOT NULL,
`username` VARCHAR(25) NOT NULL,
`password` VARCHAR(16) NOT NULL,
`email` VARCHAR(254) NOT NULL,
`birthday` DATE NULL,
`aboutMe` TEXT(300) NOT NULL,
`facebookAccount` VARCHAR(25) NULL,
`twitterAccount` VARCHAR(25) NULL,
`linkedinAccount` VARCHAR(25) NULL,
`profileImage` VARCHAR(200) NULL,
PRIMARY KEY (`userId`),
UNIQUE INDEX `username_UNIQUE` (`username` ASC),
UNIQUE INDEX `email_UNIQUE` (`email` ASC))
ENGINE = InnoDB;
```
CREATE TABLE IF NOT EXISTS `Authors`.`Books` (
`bookId` VARCHAR(100) NOT NULL,
`bookCategory` VARCHAR(25) NOT NULL,
`title` VARCHAR(25) NOT NULL,
`bookCover` VARCHAR(45) NOT NULL,
`bookDescription` VARCHAR(200) NOT NULL,
`userId` VARCHAR(100) NOT NULL,
`price` DECIMAL(2,2) NOT NULL,
`introduction` VARCHAR(300) NOT NULL,
PRIMARY KEY (`bookId`),
INDEX `userId_idx` (`userId` ASC),
CONSTRAINT `userId`
FOREIGN KEY (`userId`)
REFERENCES `Authors`.`Users` (`userId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
```
```
CREATE TABLE IF NOT EXISTS `Authors`.`BooksReview` (
`bookId` VARCHAR(100) NOT NULL,
`rateMessage` VARCHAR(100) NULL,
`rateNumber` DECIMAL(1,1) NULL,
`userId` VARCHAR(100) NOT NULL,
INDEX `userId_idx` (`userId` ASC),
CONSTRAINT `bookId`
FOREIGN KEY (`bookId`)
REFERENCES `Authors`.`Books` (`bookId`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `userId`
FOREIGN KEY (`userId`)
REFERENCES `Authors`.`Users` (`userId`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
```
CREATE TABLE IF NOT EXISTS `Authors`.`Followers` (
`follower` VARCHAR(100) NOT NULL,
`followed` VARCHAR(100) NOT NULL,
INDEX `follower_idx` (`follower` ASC),
INDEX `followed_idx` (`followed` ASC),
CONSTRAINT `follower`
FOREIGN KEY (`follower`)
REFERENCES `Authors`.`Users` (`userId`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `followed`
FOREIGN KEY (`followed`)
REFERENCES `Authors`.`Users` (`userId`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
THIS IS THE QUERY
SELECT count(br.bookId) AS reviewsCount, count(b.bookId) AS booksCount, count(f.follower) AS followersCount
FROM Users AS u
LEFT JOIN Books AS b ON b.userId = u.userId
JOIN Followers AS f ON b.userId = f.followed AND f.follower = u.userId
INNER JOIN BooksReview AS br ON br.bookId = b.bookId
AND b.bookId IN (SELECT bookId
FROM Books
WHERE userId = 'dbb21849-ccce-4af1-aa0f-6653919bf956');
我希望结果应该是 1 个关注者、4 本书和 4 条评论 但实际结果都是 0。
DML:
Users
->
userId: dbb21849-ccce-4af1-aa0f-6653919bf956
username: mostafabbbaron
等等……
Books
->
userId: dbb21849-ccce-4af1-aa0f-6653919bf956
bookId: 5f39c1ae-5e99-4b3a-8ee0-97a80c1ba9b1
等等……
Followers
->
follower: dbb21849-ccce-4af1-aa0f-6653919bf956
folllowed: b39c8e0c-4124-4339-8c30-e1fc8db5f2d4
等等……
BooksReviews
->
userId: dbb21849-ccce-4af1-aa0f-6653919bf956
bookId: aa44a455-dc28-476f-b4b9-47563a717f03
等等……
【问题讨论】:
【参考方案1】:无论哪种方式,您的查询都是错误的。但结果为零的原因可能是这些条件:b.userId = u.userId
和 b.userId = f.followed AND f.follower = u.userId
。
如果b.userId = u.userId
和b.userId = f.followed
然后f.followed = u.userId
如果f.followed = u.userId
和f.follower = u.userId
然后f.followed = f.follower
这意味着用户必须跟随自己,我怀疑是这种情况。
我会按以下方式编写查询:
SELECT
count(DISTINCT b.bookId) AS booksCount,
count(br.bookId) AS reviewsCount,
(SELECT COUNT(*) FROM Followers AS f WHERE f.followed = u.userId) AS followersCount
FROM Users AS u
LEFT JOIN Books AS b ON b.userId = u.userId
LEFT JOIN BooksReview AS br ON br.bookId = b.bookId
WHERE u.userId = 'dbb21849-ccce-4af1-aa0f-6653919bf956'
注意:虽然有Users LEFT JOIN Books LEFT JOIN BooksReview
很好,因为你有一个“关系链”Users <- Books <- BooksReview
。但是您不应该只加入Followers
表,因为它与Books
或BooksReview
没有真正的关系,也不适合该链。这就是为什么我在 SELECT 子句中使用子查询来计算关注者的原因。
【讨论】:
很高兴看到,你可以修正我的答案。但是您可以在子查询中使用u.userId
而不是硬编码的'dbb21849-ccce-4af1-aa0f-6653919bf956'
。这样可以避免重复,只需在一个地方进行更改。
感谢有用的回复,但 distinct 关键字已经删除了重复。
我的意思是你不需要像在编辑中那样在查询中写两次'dbb21849-ccce-4af1-aa0f-6653919bf956'
。以上是关于连接同一个表中的两个外键引用同一个主键的主要内容,如果未能解决你的问题,请参考以下文章