gpt4 book ai didi

mysql - 连接同一个表中的两个外键引用相同的主键

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

我正在尝试将多个表(BooksReviewFollowersBooks)与Users表连接起来Books 引用 Users 中的 userIdBooksReview 引用 userId 这是 >userId 评论了一本书,并引用了 Books 中的 bookId,这是已评论的图书 ID,也是最后但并非最不重要的 关注者 code> 表(这里我认为问题出在哪里)有两个来自同一个主键的引用,它有来自 Users 表中的 userIdfollower并且跟随也来自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:

用户 ->

       userId: dbb21849-ccce-4af1-aa0f-6653919bf956

username: mostafabbbaron

等等...

书籍 ->

       userId: dbb21849-ccce-4af1-aa0f-6653919bf956

bookId: 5f39c1ae-5e99-4b3a-8ee0-97a80c1ba9b1

等等...

关注者 ->

       follower: dbb21849-ccce-4af1-aa0f-6653919bf956

folllowed: b39c8e0c-4124-4339-8c30-e1fc8db5f2d4

等等...

图书评论 ->

       userId: dbb21849-ccce-4af1-aa0f-6653919bf956

bookId: aa44a455-dc28-476f-b4b9-47563a717f03

等等...

最佳答案

无论如何,您的查询都是错误的。但结果为零的原因可能是这些条件:b.userId = u.userIdb.userId = f.followed AND f.follower = u.userId .

IF b.userId = u.userIdb.userId = f.followed然后f.followed = u.userId

IF f.followed = u.userIdf.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 子句中使用子查询来计算关注者的数量。

关于mysql - 连接同一个表中的两个外键引用相同的主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57537009/

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