我有一个SQL查询,可以与博客作者一起拉出我所有的博客,并针对与该评论作者对应的每个博客评论执行此操作。
这3个表如下所示:
CREATE TABLE `blogs` (
`uuid` int(255) NOT NULL,
`title` varchar(1024) NOT NULL,
`detail` varchar(1024) NOT NULL,
`user_id` int(255) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `blogs`
ADD PRIMARY KEY (`uuid`),
ADD KEY `user_id` (`user_id`);
ALTER TABLE `blogs`
MODIFY `uuid` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
ALTER TABLE `blogs`
ADD CONSTRAINT `blogs_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users`
(`uuid`) ON DELETE NO ACTION ON UPDATE NO ACTION;
CREATE TABLE `blogs_comments` (
`uuid` int(255) NOT NULL,
`blog_uuid` int(255) NOT NULL,
`user_uuid` int(255) NOT NULL,
`comment` varchar(250) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `blogs_comments`
ADD PRIMARY KEY (`uuid`),
ADD KEY `user_uuid` (`user_uuid`),
ADD KEY `blog_uuid` (`blog_uuid`);
ALTER TABLE `blogs_comments`
MODIFY `uuid` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
ALTER TABLE `blogs_comments`
ADD CONSTRAINT `blogs_comments_ibfk_1` FOREIGN KEY (`blog_uuid`)
REFERENCES `blogs` (`uuid`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `blogs_comments_ibfk_2` FOREIGN KEY (`user_uuid`)
REFERENCES `users` (`uuid`) ON DELETE NO ACTION ON UPDATE NO ACTION;
COMMIT;
CREATE TABLE `users` (
`uuid` int(255) NOT NULL,
`username` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`foreName` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`surName` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`email` text COLLATE utf8_unicode_ci NOT NULL,
`number` int(11) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`is_admin` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
ALTER TABLE `users`
ADD PRIMARY KEY (`uuid`);
ALTER TABLE `users`
MODIFY `uuid` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
我当前的查询如下:
SELECT
b.uuid AS 'Blog ID', b.title AS 'Blog Title',
b.detail AS 'Blog Body', u.uuid AS 'Blog Author ID',
u.username AS 'Blog Author Username',
(SELECT bc.comment WHERE u.uuid = bc.user_uuid) AS 'Comment',
(SELECT u.uuid WHERE u.uuid = bc.user_uuid) AS 'Comment Author ID',
(SELECT u.username WHERE u.uuid = bc.user_uuid) AS 'Comment Author Username'
FROM
blogs b
INNER JOIN
blogs_comments bc
ON
b.uuid = bc.blog_uuid
INNER JOIN
users u
ON
b.user_id = u.uuid
如果博客上有评论,此查询将正常工作。如果没有评论,则博客不返回。有谁知道我该如何改变这一点,以便即使没有评论也能发布博客?
预先感谢
更新:我的PDO如下:
$stmt = (new PDO('dsn', 'user', 'pswd'))->Prepare($sql);
$stmt->execute([], PDO::FETCH_ASSOC);
var_export($stmt->fetchAll()); // Array holds nothing for blogs with no comments
答案 0 :(得分:1)
您需要一个left join
,以users
开头。但是,我认为您真正的困惑是您需要两次加入users
:
SELECT b.uuid AS Blog_ID,
b.title AS Blog_Title,
b.detail AS Blog_Body,
u.uuid AS Blog_Author_ID,
u.username AS Blog_Author_Username,
bc.comment AS Comment,
bc.user_uuid AS Comment_Author_ID,
ubc.username AS Comment_Author_Username
FROM users u LEFT JOIN
blogs b
ON b.user_id = u.uuid LEFT JOIN
blogs_comments bc
ON b.uuid = bc.blog_uuid LEFT JOIN
users ubc
ON bc.user_uuid = u.uuid;
注意:
users
。SELECT
中使用没有FROM
子句的子查询。答案 1 :(得分:0)
LEFT OUTER JOIN
blogs_comments bc
应该这样做。