I'm having trouble wrapping my head around a query with a subquery. Follow along, if you will:
SAMPLE DATA:
CREATE TABLE users (
user_id INT(10) NOT NULL AUTO_INCREMENT,
username VARCHAR(64),
PRIMARY KEY (user_id)
);
CREATE TABLE cars {
car_id INT(10) NOT NULL AUTO_INCREMENT,
user_id INT(10),
caryear YEAR(4),
carmaker VARCHAR(32),
carmodel VARCHAR(32),
PRIMARY KEY (car_id, user_id)
FOREIGN KEY(`user_id`) references users(`user_id`)
) ENGINE=InnoDB;
INSERT INTO users (user_id, name) VALUES (1,'Bob'),(2,'John'),(3,'Sally');
INSERT INTO cars (user_id, caryear, carmaker, carmodel) VALUES
(1,'2004','Audi','A4'),
(1,'2006','Toyota','Camry'),
(1,'2014','Jeep','CJ'),
(2,'1998','Acura','CL'),
(2,'2014','Honda','Accord'),
(3,'2011','Jeep','Rubicon')
Okay, so if I want to get a list of users and their cars, I can do something like this:
SELECT
user_id,
username,
(SELECT GROUP_CONCAT(CONCAT(CarYear,' ',CarMaker,' ',CarModel))
FROM cars
WHERE cars.user_id = users.user_id
) AS Cars
FROM users;
But how can I get a list of users that own a 2004-2014 Jeep, for instance?
I've come up with this, but I'm sure there is a more elegant approach:
SELECT
user_id,
username,
(SELECT GROUP_CONCAT(CONCAT(CarYear,' ',CarMaker,' ',CarModel))
FROM cars
WHERE cars.user_id = users.user_id
AND CarMaker = 'Jeep'
AND CarYear BETWEEN 2004 AND 2014
) AS Cars
FROM users
HAVING Cars is not null;
The problem with this solution is that it does not show the other cars owned by people who own 2004-2014 Jeeps.
Any advice?