I am using MySQL Database and I have two tables. They are User and Reservation
Here is my question.
- Currently, I used
LEFT JOIN how about SubQuery with NOT EXIST . which is better in terms of performance? - Can I create views for this query, can it make any difference in performance
User
| FIELD | TYPE | NULL | KEY | DEFAULT | EXTRA | |-------|-------------|------|-----|---------|----------------| | uid | int(11) | NO | PRI | (null) | auto_increment | | uname | varchar(30) | YES | | (null) | |
Reservation
| 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 code:
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) )
My current working SQL query
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
user978494
source share