mysql - SQL - 基本内连接查询

标签 mysql

我有 3 个 MySQL 表,我想将它们连接在一起。


类(class):

CREATE TABLE `library_classes` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

INSERT INTO `library_classes` (`id`, `name`) VALUES
(1, 'Tolkien'),
(2, 'CS Lewis');

session :

CREATE TABLE `library_sessions` (
  `id` int NOT NULL AUTO_INCREMENT,
  `class_id` int NOT NULL,
  `session_timestamp` timestamp NOT NULL,
  PRIMARY KEY (`id`),
  KEY `class_id` (`class_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

INSERT INTO `library_sessions` (`id`, `class_id`, `session_timestamp`) VALUES
(1, 1,  '2023-08-01 15:30:00'),
(2, 2,  '2023-08-02 10:15:00'),
(3, 1,  '2023-08-04 09:30:00');

预订:

CREATE TABLE `library_bookings` (
  `id` int NOT NULL AUTO_INCREMENT,
  `session_id` int NOT NULL,
  `email` varchar(255) NOT NULL,
  `datetime_submitted` timestamp NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

INSERT INTO `library_bookings` (`id`, `session_id`, `email`) VALUES
(1, 1,  '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="513b3e223411363c30383d7f323e3c" rel="noreferrer noopener nofollow">[email protected]</a>'),
(2, 2,  '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d4beb5bab194adb5bcbbbbfab7bbb9" rel="noreferrer noopener nofollow">[email protected]</a>'),
(3, 2,  '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="771d181f19371f18031a161e1b5914181a" rel="noreferrer noopener nofollow">[email protected]</a>');

何时 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="660c09150326010b070f0a4805090b" rel="noreferrer noopener nofollow">[email protected]</a>已登录,我希望我的页面如下所示:

Tolkien | 2023-08-01 15:30:00 | CANCEL
CS Lewis | 2023-08-02 10:15:00 | BOOK NOW
Tolkien | 2023-08-04 09:30:00 | BOOK NOW

最佳答案

我已将您的数据导入到 SQLFIDDLE ;

以下查询将为您提供预期的结果。

select lc.name, ls.session_timestamp,
if(lb.id is null, 'BOOK NOW', 'CANCEL') as booking_status
from library_classes lc
join library_sessions ls on lc.id = ls.class_id
left join library_bookings lb on ls.id = lb.session_id and lb.email = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="117b7e627451767c70787d3f727e7c" rel="noreferrer noopener nofollow">[email protected]</a>'
order by ls.session_timestamp;

它给出的预期输出为:

name        session_timestamp       booking_status
Tolkien     2023-08-01T15:30:00Z    CANCEL
CS Lewis    2023-08-02T10:15:00Z    BOOK NOW
Tolkien     2023-08-04T09:30:00Z    BOOK NOW

关于mysql - SQL - 基本内连接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75684833/

相关文章:

php - 使用 'into outfile' 查询时如何在数据库表中添加自定义列

mysql - 如何告诉gorm保存丢失的时间。时间字段为NULL而不是 '0000-00-00'?

java - 优化数据库搜索查询

MySQL "create trigger tr_emp before insert or update"

mysql - SQL 查询从列中获取不同的数据并在结果中获取其余数据

php - php中mysql查询错误

php - 为什么不自动创建关系表? (在mysql中)

php - Laravel 5 并发请求和预定作业

mysql - 引发 MySQL 读/写超时

mysql - 多连接SQL查询优化