Avoid duplicate entries when joining multiple tables (MySQL) - mysql

(please see the database structure I'm testing with at the bottom of this post.)
I execute this query:
SELECT m.title, GROUP_CONCAT(DISTINCT(d.name) SEPARATOR ',') d FROM movies m
INNER JOIN movies_seen s
ON s.object_id = m.id
LEFT JOIN movies_directors_connections dc
ON dc.movie_id = m.id
LEFT JOIN movies_directors d
ON d.id = dc.director_id
With this result:
title | d
Pulp Fiction | Quentin Tarantino,George Butler,Robert Fiore
But I'm trying to get this:
title | d
Pulp Fiction | Quentin Tarantino
Pumping Iron | George Butler,Robert Fiore
And suggestions? :)
CREATE TABLE `movies` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(90) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 ;
CREATE TABLE `movies_seen` (
`object_id` int(10) NOT NULL DEFAULT '0',
`date` varchar(10) NOT NULL DEFAULT '0');
CREATE TABLE `movies_directors` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 ;
CREATE TABLE IF NOT EXISTS `movies_directors_connections` (
`movie_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
`director_id` mediumint(8) unsigned NOT NULL DEFAULT '0'
) ENGINE=MyISAM;
And then some test data:
INSERT INTO `movies` (`id`, `title`) VALUES
(1, 'Pulp Fiction'), (2, 'Pumping Iron');
INSERT INTO `movies_seen` (`object_id`, `date`) VALUES
(1, 1359511222), (2, 1359511223);
INSERT INTO `movies_directors` (`id`, `name`) VALUES
(1, 'Quentin Tarantino'),
(2, 'George Butler'),
(3, 'Robert Fiore');
INSERT INTO `movies_directors_connections` (`movie_id`, `director_id`) VALUES
(1, 1), (2, 2), (2, 3);

you just need to add GROUP BY clause
SELECT m.title,
GROUP_CONCAT(DISTINCT(d.name) SEPARATOR ',') d
FROM movies m
INNER JOIN movies_seen s
ON s.object_id = m.id
LEFT JOIN movies_directors_connections dc
ON dc.movie_id = m.id
LEFT JOIN movies_directors d
ON d.id = dc.director_id
GROUP BY m.title
SQLFiddle Demo
OTHER LINK
MySQL GROUP BY clause

Related

Joining data from 3 separate MySQL tables

I have 3 MySQL tables: person, review & team.
I have been able to join 2 (person & review) together, however I'd like to include data from the 3rd in my result.
Can someone explain how this is done? :-)
CREATE TABLE `person` (
`id` int NOT NULL AUTO_INCREMENT,
`reference` varchar(100) NOT NULL,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
INSERT INTO `person` (`id`, `reference`, `email`) VALUES
(1, 'PK001', 'paulk#gmail.com');
CREATE TABLE `review` (
`id` int NOT NULL AUTO_INCREMENT,
`review_type` varchar(255) NOT NULL,
`review_body` varchar(255) NOT NULL,
`person_id` int NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
INSERT INTO `review` (`id`, `review_type`, `review_body`, `person_id`) VALUES
(1, 'Personality', 'He has a great personality!', 1),
(2, 'Skills', 'He has multiple skills!', 1);
CREATE TABLE `team` (
`id` int(11) NOT NULL,
`person_id` int(11) NOT NULL,
`team_name` varchar(255) NOT NULL,
`value` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
INSERT INTO `team` (`id`, `person_id`, `team_name`, `value`) VALUES
(1, 1, 'Man Utd', 500),
(2, 1, 'Real Madrid', 1500),
(3, 1, 'Ajax', 1000);
Using the following SQL:
SELECT p.id, group_concat(r.review_body)
FROM person p
inner join review r on r.person_id = p.id
group by p.id
gives me the output:
He has a great personality!,He has multiple skills!
However I'd ultimately like my output to be:
He has multiple skills!,He has a great personality,Man Utd-500|Real Madrid-1500|Ajax-1000
Is this possible to do with MySQL ? Any guidance would be greatly appreciated.
I realise I could optimise things a lot better - but I just want to see if I can connect all 3 tables together and go from there.
To get your required concatenated output you need to modify your join query.
For that your new query looks like this:
SELECT p.id,
GROUP_CONCAT(r.review_body) AS reviews,
(SELECT GROUP_CONCAT(CONCAT(team_name, '-', value) SEPARATOR '|')
FROM team
WHERE team.person_id = p.id) AS teams
FROM person p
INNER JOIN review r ON r.person_id = p.id
GROUP BY p.id;
Result :

mysql - find all students of a class if at least one student attended

I have four tables to map students and classes they attend, and to keep attendance info with start and end times.
Reproducible table schemas with records:
CREATE TABLE IF NOT EXISTS `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB;
INSERT INTO `student` (`id`, `name`) VALUES
(1, 'student 1'),
(2, 'student 2'),
(3, 'student 3'),
(4, 'student 4'),
(5, 'student 5');
CREATE TABLE IF NOT EXISTS `class` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB;
INSERT INTO `class` (`id`, `name`) VALUES
(1, 'class 1'),
(2, 'class 2');
CREATE TABLE IF NOT EXISTS `student_class` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) DEFAULT NULL,
`class_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`student_id`) REFERENCES `student` (`id`),
FOREIGN KEY (`class_id`) REFERENCES `class` (`id`)
)ENGINE=InnoDB;
INSERT INTO `student_class` (`id`, `student_id`, `class_id`) VALUES
(1, 1, 1),
(2, 2, 1),
(3, 3, 1),
(4, 4, 2),
(5, 5, 2);
CREATE TABLE IF NOT EXISTS `attendance` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_class_id` int(11) DEFAULT NULL,
`start_time` time DEFAULT NULL,
`end_time` time DEFAULT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`student_class_id`) REFERENCES `student_class` (`id`)
)ENGINE=InnoDB;
INSERT INTO `attendance` (`id`, `student_class_id`, `start_time`, `end_time`) VALUES
(1, 1, '09:00:00', '10:00:00');
Problem:
I need to list down rows which only shows all attendance of a class to which at least one student attended,
(even if the rest of students have null for start_time, end_time).
Here is my current sql:
SELECT c.id classId, sc.id mapperId, a.start_time startTime, a.end_time endTime FROM class c
JOIN student_class sc ON sc.class_id = c.id
LEFT JOIN attendance a ON a.student_class_id = sc.id;
The result should look like this.
classId
mapperId
startTime
endTime
1
1
09:00:00
10:00:00
1
2
NULL
NULL
1
3
NULL
NULL
#user you can get the result set you are looking for my using another instance of your query modified slightly as a sub query, like this:
SELECT c.id classId,
sc.id mapperId,
a.start_time startTime,
a.end_time endTime
FROM class c
JOIN student_class sc ON sc.class_id = c.id
LEFT JOIN attendance a ON a.student_class_id = sc.id
WHERE `c`.`id` IN (
SELECT DISTINCT c.id classId
FROM class c
JOIN student_class sc ON sc.class_id = c.id
JOIN attendance a ON a.student_class_id = sc.id
)
Here is a mock up of the answer on sqlfiddle.com
Just add a condition for the class id
SELECT c.id classId, sc.id mapperId, a.start_time startTime,
a.end_time endTime FROM class c
JOIN student_class sc ON sc.class_id = c.id
LEFT JOIN attendance a ON a.student_class_id = sc.id
where c.id in (select distinct sc.class_id
from attendance a
join student_class sc
on a.student_class_id = sc.id);
Based on your question answer will be like this
SELECT cls.id, stdclass.id AS mapperId , att.start_time ,att.end_time FROM attendance att INNER JOIN student_class stdclass ON att.student_class_id = stdclass.id INNER JOIN class cls ON stdclass.class_id = cls.id INNER JOIN student std ON stdclass.student_id = std.id

MySQL: Select records where joined table matches ALL values

I'm trying to find all employees with multiple skills. Here are the tables:
CREATE TABLE IF NOT EXISTS `Employee` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
INSERT INTO `Employee` (`ID`, `Name`, `Region_ID`) VALUES (1, 'Fred Flintstone'), (2, 'Barney Rubble');
CREATE TABLE IF NOT EXISTS `Skill` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
INSERT INTO `Skill` (`ID`, `Name`) VALUES (1, 'PHP'), (2, 'JQuery');
CREATE TABLE IF NOT EXISTS `Emp_Skills` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`Emp_ID` bigint(20) unsigned NOT NULL DEFAULT '0',
`Skill_ID` bigint(20) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
INSERT INTO `Emp_Skills` (`ID`, `Emp_ID`, `Skill_ID`) VALUES (1, 1, 1), (2, 1, 2), (3, 2, 1);
Here is the query I have so far:
SELECT DISTINCT(em.ID), em.Name
FROM Employee em
INNER JOIN Emp_Skills es ON es.Emp_ID = em.ID
WHERE es.Skill_ID IN ('1', '2')
This returns both employees, however, I need to find the employee that has both skills (ID 1 and 2).
Any ideas? Thanks
This will do it:
SELECT EmpId, Name
FROM
(
SELECT em.ID as EmpId, em.Name, es.ID as SkillID
FROM Employee em
INNER JOIN Emp_Skills es ON es.Emp_ID = em.ID
WHERE es.Skill_ID IN ('1', '2')
) X
GROUP BY EmpID, Name
HAVING COUNT(DISTINCT SkillID) = 2;
Fiddle here:
The distinct is just in case the same employee has the skill listed twice.
Thanks for the test data.
You can do this with aggregation and a having clause:
SELECT em.ID, em.Name
FROM Employee em INNER JOIN
Emp_Skills es
ON es.Emp_ID = em.ID
GROUP BY em.id, em.name
HAVING sum(es.Skill_id = '1') > 0 and
sum(es.Skill_id = '2') > 0;
Each condition in the having clause counts the number of rows for each employee that have a particular skill. The filter guarantees that both skills are present.

Get data sorted even if some references are NULL

I want to order my results after a name. Thereby multiple tables are necessary. Now I have the problem that I want to sort the name even if there is a null in the column. Below you find a sample database which should represent the problem:
My tables
CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`desc` varchar(255) NOT NULL,
`price` decimal(10,0) NOT NULL,
`manufacturer_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
CREATE TABLE IF NOT EXISTS `manufacturer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`desc` varchar(255) NOT NULL,
`website` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
My data:
INSERT INTO `products` (`id`, `desc`, `price`, `manufacturer_id`) VALUES
(1, 'book', 12, 1),
(2, 'cup', 4, 2),
(3, 'Arbitrary product', 100, NULL);
INSERT INTO `manufacturer` (`id`, `title`, `desc`, `website`) VALUES
(1, 'Publisher', 'Lorem ipsum', 'www.stackoverflow.com'),
(2, 'Cup producer', 'Lorem ipsum', 'www.cup.com');
If I do a SELECT * FROM products than I would get three results. If I want to order it I have a query like
SELECT p.desc, p.price, m.title
FROM products p, manufacturer m
WHERE p.manufacturer_id = m.id
ORDER BY m.title
This gives me only two results because of the null value in products. Is it possible to sort the table products after the manufacturer title even there is a null in it?
You did a join by saying p.manufacturer_id = m.id
But you didn't specify it so by default it's an inner join,
You want to have a left join where your 'left' table is the products table
SELECT p.desc, p.price, m.title
FROM products AS p
LEFT JOIN manufacturer AS m ON m.id = p.manufacturer_id
ORDER BY m.title
Have a look at this image for a better understanding
http://www.codeproject.com/KB/database/Visual_SQL_Joins/Visual_SQL_JOINS_V2.png
Use LEFT JOIN
Try this:
SELECT p.desc, p.price, m.title
FROM products p LEFT OUTER JOIN manufacturer m
ON p.manufacturer_id = m.id
ORDER BY m.title

MySQL count N consecutive days in normal form tables

I want to count N consecutive days that a specific user has meetings, on a given date and before it.
For example: count the consecutive meeting days that a user with id 1 has at 16 January 2013.
I found some good answers here and here but the tables are not in normal form like my sample above and i cannot figure out how to implement it for my occasion.
A sample table structure as follows:
CREATE TABLE IF NOT EXISTS `meetings` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`time` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `meetings_users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned NOT NULL,
`meeting_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `meeting_id` (`meeting_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Constraints for table `meetings_users`
--
ALTER TABLE `meetings_users`
ADD CONSTRAINT `meetings_users_ibfk_2` FOREIGN KEY (`meeting_id`) REFERENCES `meetings` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `meetings_users_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Sample inserts
INSERT INTO `users` ( `id` ) VALUES (1)
INSERT INTO `meetings` ( `id`, `time` ) VALUES
(1, '2013-01-14 10:00:00'),
(2, '2013-01-15 10:00:00'),
(3, '2013-01-16 10:00:00')
INSERT INTO `meetings_users` ( `id`, `meeting_id`, `user_id` ) VALUES
(1, 1, 1),
(2, 2, 1),
(3, 3, 1)
Desired output:
*+---------+-----------------+
| user_id | consecutive_days |
+---------+------------------+
| 1 | 3 |
+---------+------------------+
How about something like this. I expect it can be re-written without the subqueries but I must be having a bit of a brain freeze... (data set and query amended to suit shifting requirements)
DROP TABLE IF EXISTS meetings;
CREATE TABLE IF NOT EXISTS meetings
( meeting_id int(10) unsigned NOT NULL AUTO_INCREMENT
, meeting_time datetime NOT NULL
, PRIMARY KEY (meeting_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS meetings_users;
CREATE TABLE IF NOT EXISTS meetings_users
( user_id int(10) unsigned NOT NULL
, meeting_id int(10) unsigned NOT NULL
, PRIMARY KEY (meeting_id,user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS users;
CREATE TABLE IF NOT EXISTS users
( user_id int(10) unsigned NOT NULL AUTO_INCREMENT
, PRIMARY KEY (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO users ( user_id ) VALUES (1),(2),(3),(4);
INSERT INTO meetings ( meeting_id, meeting_time ) VALUES
(1, '2013-01-14 10:00:00'),
(2, '2013-01-15 10:00:00'),
(3, '2013-01-16 10:00:00'),
(4, '2013-01-17 10:00:00'),
(5, '2013-01-18 10:00:00'),
(6, '2013-01-19 10:00:00'),
(7, '2013-01-20 10:00:00'),
(8, '2013-01-14 12:00:00');
INSERT INTO meetings_users (meeting_id, user_id ) VALUES
(1, 1),
(2, 1),
(2, 3),
(3, 1),
(3, 3),
(4, 2),
(4, 3),
(5, 2),
(6, 1),
(1, 8);
SET #dt = '2013-01-15';
SELECT user_id
, start
, DATEDIFF(#dt,start)+1 cons
FROM
(
SELECT a.user_id
, a.meeting_date Start
, MIN(c.meeting_date) End
, DATEDIFF(MIN(c.meeting_date),a.meeting_date) + 1 diff
FROM (SELECT DISTINCT mu.user_id,DATE(m.meeting_time) meeting_date FROM meetings_users mu JOIN meetings m ON m.meeting_id = mu.meeting_id) a
LEFT
JOIN (SELECT DISTINCT mu.user_id,DATE(m.meeting_time) meeting_date FROM meetings_users mu JOIN meetings m ON m.meeting_id = mu.meeting_id) b
ON b.user_id = a.user_id
AND a.meeting_date = b.meeting_date + INTERVAL 1 DAY
LEFT
JOIN (SELECT DISTINCT mu.user_id,DATE(m.meeting_time) meeting_date FROM meetings_users mu JOIN meetings m ON m.meeting_id = mu.meeting_id) c
ON c.user_id = a.user_id
AND a.meeting_date <= c.meeting_date
LEFT
JOIN (SELECT DISTINCT mu.user_id,DATE(m.meeting_time) meeting_date FROM meetings_users mu JOIN meetings m ON m.meeting_id = mu.meeting_id) d
ON d.user_id = a.user_id
AND c.meeting_date = d.meeting_date - INTERVAL 1 DAY
WHERE b.meeting_date IS NULL
AND c.meeting_date IS NOT NULL
AND d.meeting_date IS NULL
GROUP
BY a.user_id
, a.meeting_date
) x
WHERE #dt BETWEEN start AND end;
+---------+------------+------+
| user_id | start | cons |
+---------+------------+------+
| 1 | 2013-01-14 | 2 |
| 3 | 2013-01-15 | 1 |
+---------+------------+------+