SQL – 基本的内连接查询

huangapple go评论58阅读模式
英文:

SQL - basic Inner Join query

问题

以下是翻译好的部分:

Classes:

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');

Sessions:

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');

Bookings:

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, 'jose@gmail.com'),
(2, 2, 'jane@yahoo.com'),
(3, 2, 'john@hotmail.com');

当用户 "jose@gmail.com" 登录时,希望我的页面显示如下内容:

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
英文:

I have 3 MySQL tables below which I'd like to join together.


Classes:

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');

Sessions:

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');

Bookings:

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,	'jose@gmail.com'),
(2,	2,	'jane@yahoo.com'),
(3, 2,	'john@hotmail.com');

When jose@gmail.com is signed in, I'd like my page to look like the following:

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

答案1

得分: 2

I imported your data to SQLFIDDLE;

The following query shall get you the expected result.

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 = 'jose@gmail.com'
order by ls.session_timestamp;

It gives the expected output as :

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
英文:

I imported your data to SQLFIDDLE;

The following query shall get you the expected result.

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 = 'jose@gmail.com'
order by ls.session_timestamp;

It gives the expected output as :

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

huangapple
  • 本文由 发表于 2023年3月9日 20:47:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定