SQL DISTINCT when combining LEFT JOIN with subquery
I have the following two SQL tables for storing movie titles, and the dates I've seen them:
CREATE TABLE movies (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
title varchar(90) NOT NULL,
year varchar(7) DEFAULT NULL,
watchAgain enum('0', '1') NOT NULL DEFAULT '0',
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
INSERT INTO movies (id, title, year, watchAgain)
VALUES (1, 'Wall Street', '1987', '1'), (2, 'Wag the Dog', '1997', '1');
CREATE TABLE movies_seen (
object_id int(10) NOT NULL DEFAULT '0',
date varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci
NOT NULL DEFAULT '0',
seen_again enum('0', '1') NOT NULL DEFAULT '0',
PRIMARY KEY (object_id, date)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `movies_seen` (object_id, date, seen_again)
VALUES (1, '1156187700', '0'), (1, '1218395700', '0'), (2, '0', '0');
To find movies I should watch again, I ask the database for titles with seen_again == 1
, or if seen_again == 0
and it is not stored in movies_seen
:
SELECT m.year, m.title, s.date AS last_seen
FROM movies m
LEFT JOIN movies_seen s ON m.id = s.object_id
WHERE
(
m.watchAgain = '0'
AND m.id NOT IN (SELECT s.开发者_如何学Cobject_id
FROM movies m
INNER JOIN movies_seen s ON m.id = s.object_id)
OR m.watchAgain = '1'
)
ORDER BY m.title
This basically works, but when the movies_seen table has 2 entires for the same title, it shows up duplicated in the results:
year title last_seen
1997 Wag the Dog 0
1987 Wall Street 1156187700
1987 Wall Street 1218395700
It works with DISTINCT if I only fetch the title, but as I need additional columns, this obviously fails. Ideas?
In MySQL a GROUP BY
clause can do the same task as a DISTINCT
SELECT m.year
, m.title
, s.date AS last_seen
FROM movies m
LEFT JOIN movies_seen s ON m.id = s.object_id
WHERE (m.watchAgain = '0'
AND m.id NOT IN (SELECT s.object_id
FROM movies m
INNER JOIN movies_seen s ON m.id = s.object_id)
OR m.watchAgain = '1')
GROUP BY m.title /*<<--- this will do a distinct on title only*/
ORDER BY m.title
AFAIK, this only works on MySQL.
BTW: Since you are grouping on m.title MySQL does a sort on title, so the ORDER BY
clause is not technically needed anymore.
SELECT m.year, m.title, s.date AS last_seen
FROM movies m
WHERE exists
(
select 1 from movies_seen s
where m.id = s.object_id
and ( m.watchAgain = '0'
AND m.id NOT IN (SELECT s.object_id FROM movies m INNER JOIN movies_seen s ON m.id = s.object_id)
OR m.watchAgain = '1'
)
)
ORDER BY m.title
精彩评论