我正在使用MySQL
数据库,我有两个表。他们是User
并且Reservation
这是我的问题。
- 目前,我将
LEFT JOIN
SubQuery 与NOT EXIST
. 从性能的角度来看,哪个更好? - 我可以为此查询创建视图吗,这会对性能产生任何影响吗
用户
| FIELD | TYPE | NULL | KEY | DEFAULT | EXTRA |
|-------|-------------|------|-----|---------|----------------|
| uid | int(11) | NO | PRI | (null) | auto_increment |
| uname | varchar(30) | YES | | (null) | |
预订
| FIELD | TYPE | NULL | KEY | DEFAULT | EXTRA |
|----------|-----------|------|-----|-------------------|----------------|
| rid | int(11) | NO | PRI | (null) | auto_increment |
| uid | int(11) | YES | MUL | (null) | |
| reserved | timestamp | NO | | CURRENT_TIMESTAMP | |
SQL 代码:
create table user (
uid int not null auto_increment,
uname varchar(30),
primary key(uid)
);
create table reservation (
rid int not null auto_increment,
uid int,
reserved timestamp not null default CURRENT_TIMESTAMP,
primary key(rid),
foreign key (uid) references user (uid)
)
我当前工作的 SQL 查询
SELECT u.uid, u.uname, date_format(reserved, '%Y%m')
FROM user as u
LEFT JOIN reservation as r on
r.uid = u.uid and date_format(reserved, '%Y%m') = 201307
where r.uid is null