Hey Guys I need Help with an SQL statement. There are 5 tables total and I need to join multiple tables for a SELECT statement.
These are the tables:
+-----------+------------+------------+
| person_id | first_name | last_name |
+-----------+------------+------------+
| 1 | Tom | Hanks |
| 2 | Anne | Hathaway |
| 3 | Tom | Cruise |
| 4 | Meryl | Streep |
| 5 | Chris | Pratt |
| 6 | Halle | Berry |
| 7 | Robert | De Niro |
| 8 | Julia | Roberts |
| 9 | Denzel | Washington |
| 10 | Melissa | McCarthy |
+-----------+------------+------------+
+-------------+----------------------+
| building_id | building_name |
+-------------+----------------------+
| 1 | Headquarters |
| 2 | Main Street Building |
+-------------+----------------------+
+---------+-------------+-------------+----------+
| room_id | room_number | building_id | capacity |
+---------+-------------+-------------+----------+
| 1 | 100 | 1 | 5 |
| 2 | 200 | 1 | 4 |
| 3 | 300 | 1 | 10 |
| 4 | 10 | 2 | 4 |
| 5 | 20 | 2 | 4 |
+---------+-------------+-------------+----------+
+------------+---------+---------------------+---------------------+
| meeting_id | room_id | meeting_start | meeting_end |
+------------+---------+---------------------+---------------------+
| 1 | 1 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
| 2 | 1 | 2016-12-25 10:00:00 | 2016-12-25 12:00:00 |
| 3 | 1 | 2016-12-25 11:00:00 | 2016-12-25 12:00:00 |
| 4 | 2 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
| 5 | 4 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
| 6 | 5 | 2016-12-25 14:00:00 | 2016-12-25 16:00:00 |
+------------+---------+---------------------+---------------------+
+-----------+------------+
| person_id | meeting_id |
+-----------+------------+
| 1 | 1 |
| 10 | 1 |
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 2 |
| 5 | 2 |
| 6 | 2 |
| 7 | 2 |
| 8 | 2 |
| 9 | 3 |
| 10 | 3 |
| 1 | 4 |
| 2 | 4 |
| 8 | 5 |
| 9 | 5 |
| 1 | 6 |
| 2 | 6 |
| 3 | 6 |
+-----------+------------+
The problem:
Construct the SQL statement to find all the meetings that Tom Hanks has to attend. Display the following columns:
Person’s first name
Person’s last name
Building name
Room number
Meeting start date and time
Meeting end date and time
My statement:
SELECT person.first_name, person.last_name, building.building_name,
->
-> room.room_number, meeting.meeting_start, meeting.meeting_end
->
-> FROM person
->
-> JOIN building
->
-> ON person.person_id = building.building_id
->
-> JOIN room
->
-> ON person.person_id = room.room_id
->
-> JOIN meeting
->
-> ON person.person_id = meeting.meeting_id
->
-> WHERE person_id = 1;
RESULT:
+------------+-----------+---------------+-------------+---------------------+---------------------+
| first_name | last_name | building_name | room_number | meeting_start | meeting_end |
+------------+-----------+---------------+-------------+---------------------+---------------------+
| Tom | Hanks | Headquarters | 100 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
+------------+-----------+---------------+-------------+---------------------+---------------------+
Tom Hanks has multiple meetings.(4 to be exact) How do I show the 4 meetings? Not sure what to do here. It is only showing 1 with my statement.
Was able to find a solution. JOIN was not needed...
SELECT first_name,last_name ,building_name,room_number ,meeting_start,meeting_end
FROM person P, building B,person_meeting PM,meeting M,room R
WHERE
P.person_id=PM.person_id
AND M.room_id=R.room_id
AND B.building_id=R.building_id
AND PM.meeting_id=M.meeting_id
AND P.first_name='Tom'
AND P.last_name='Hanks';
This query should give you what you want (barring any spelling mistakes :P!)
SELECT person.first_name,
person.last_name,
building.building_name,
room.room_number,
meeting.meeting_start,
meeting.meeting_end
FROM person
INNER JOIN person_meeting ON person_meeting.person_id = person.person_id
INNER JOIN meeting ON meeting.meeting_id = person_meeting.meeting_id
INNER JOIN room ON room.room_id = meeting.room_id
INNER JOIN building on building.building_id = room.building_id
WHERE person.person_id = 1
With this query you are joining the tables on the relationships you've created.
A person is linked to meeting via meeting.person_id = person.person_id a meeting is linked to a room via room.room_id = meeting.room_id and a room is linked to a building via building.building_id = room.room_id then finally filtering down these to only show where person.person_id = 1
The issue with your query was you tried to join everything onto person via person_id
JOIN building ON person.person_id = building.building_id
+-------------+----------------------+
| building_id | building_name |
+-------------+----------------------+
| 1 | Headquarters |<-
| 2 | Main Street Building |
+-------------+----------------------+
from this table we will only get building_id = 1
JOIN room ON person.person_id = room.room_id
+---------+-------------+-------------+----------+
| room_id | room_number | building_id | capacity |
+---------+-------------+-------------+----------+
| 1 | 100 | 1 | 5 | <-
| 2 | 200 | 1 | 4 |
| 3 | 300 | 1 | 10 |
| 4 | 10 | 2 | 4 |
| 5 | 20 | 2 | 4 |
+---------+-------------+-------------+----------+
from this table we will only get room_id = 1
JOIN meeting ON person.person_id = meeting.meeting_id
+------------+---------+---------------------+---------------------+
| meeting_id | room_id | meeting_start | meeting_end |
+------------+---------+---------------------+---------------------+
| 1 | 1 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 | <-
| 2 | 1 | 2016-12-25 10:00:00 | 2016-12-25 12:00:00 |
| 3 | 1 | 2016-12-25 11:00:00 | 2016-12-25 12:00:00 |
| 4 | 2 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
| 5 | 4 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
| 6 | 5 | 2016-12-25 14:00:00 | 2016-12-25 16:00:00 |
+------------+---------+---------------------+---------------------+
from this table we can see we will only get meeting_id = 1
Primary Keys/Foreign Keys
Person.person_id is your PK and meeting.person_id is your FK
room.room_id is your PK and meeting.room_id is your FK
Building.building_id is your PK and room.building_id is your FK
SQL Fiddle
Related
I'm trying to come up with a stored procedure that takes multiple rows that are exactly identical, and combines them into one row while summing one column, which can then be run through more stored procedures based on the sum of that one column.
I've tried a GROUP BY statement, but that doesn't actually group the rows together, because if I run the table through another procedure it performs actions as if each row were not combined. Performing a SELECT * FROM mytable query shows that each row was not actually combined into one.
Is there any way to permanently combine multiple rows into one singular row?
To start, I've got a table like this:
+-------+-----+--------+---------+------+-----+-----------+
| RowID | pID | Name | Date | Code | QTY | Purchased |
+-------+-----+--------+---------+------+-----+-----------+
| 1 | 1 | bob | 9/29/20 | 123 | 1 | |
| 2 | 1 | bob | 8/10/20 | 456 | 1 | |
| 3 | 2 | rob | 9/15/20 | 123 | 1 | |
| 4 | 2 | rob | 9/15/20 | 123 | 1 | |
| 5 | 2 | rob | 9/15/20 | 123 | 1 | |
| 6 | 2 | rob | 9/15/20 | 123 | 1 | |
| 7 | 2 | rob | 9/15/20 | 123 | 1 | |
| 8 | 3 | john | 7/12/20 | 987 | 1 | |
| 9 | 3 | john | 7/12/20 | 987 | 1 | |
| 10 | 4 | george | 9/12/20 | 684 | 1 | |
| 11 | 5 | paul | 2/2/20 | 454 | 1 | |
| 12 | 6 | amy | 1/12/20 | 252 | 1 | |
| 13 | 7 | susan | 5/30/20 | 131 | 1 | |
| 14 | 7 | susan | 6/6/20 | 252 | 1 | |
| 15 | 7 | susan | 5/30/20 | 131 | 1 | |
+-------+-----+--------+---------+------+-----+-----------+
By the end, i'd like to have a table like this:
+-------+-----+--------+---------+------+-----+-----------+
| RowID | pID | Name | Date | Code | QTY | Purchased |
+-------+-----+--------+---------+------+-----+-----------+
| 1 | 1 | bob | 9/29/20 | 123 | 1 | |
| 2 | 1 | bob | 8/10/20 | 456 | 1 | |
| 3 | 2 | rob | 9/15/20 | 123 | 5 | |
| 4 | 3 | john | 7/12/20 | 987 | 2 | |
| 5 | 4 | george | 9/12/20 | 684 | 1 | |
| 6 | 5 | paul | 2/2/20 | 454 | 1 | |
| 7 | 6 | amy | 1/12/20 | 252 | 1 | |
| 8 | 7 | susan | 5/30/20 | 131 | 2 | |
| 9 | 7 | susan | 6/6/20 | 252 | 1 | |
+-------+-----+--------+---------+------+-----+-----------+
Where exactly identical rows are combined into one row, and the QTY field is summed, that I can then add purchases to, or make deductions from the quantity as a total. Using GROUP BY statements can achieve this, but when I go to alter the quantity or add purchases to each person, it treats it like the first table, as if nothing was actually grouped.
So you have this table:
| RowID | pID | Name | Date | Code | QTY | Purchased |
+-------+-----+--------+---------+------+-----+-----------+
| 1 | 1 | bob | 9/29/20 | 123 | 1 | |
| 2 | 1 | bob | 8/10/20 | 456 | 1 | |
| 3 | 2 | rob | 9/15/20 | 123 | 1 | |
| 4 | 2 | rob | 9/15/20 | 123 | 1 | |
| 5 | 2 | rob | 9/15/20 | 123 | 1 | |
| 6 | 2 | rob | 9/15/20 | 123 | 1 | |
| 7 | 2 | rob | 9/15/20 | 123 | 1 | |
| 8 | 3 | john | 7/12/20 | 987 | 1 | |
| 9 | 3 | john | 7/12/20 | 987 | 1 | |
| 10 | 4 | george | 9/12/20 | 684 | 1 | |
| 11 | 5 | paul | 2/2/20 | 454 | 1 | |
| 12 | 6 | amy | 1/12/20 | 252 | 1 | |
| 13 | 7 | susan | 5/30/20 | 131 | 1 | |
| 14 | 7 | susan | 6/6/20 | 252 | 1 | |
| 15 | 7 | susan | 5/30/20 | 131 | 1 | |
The best way, as has been suggested, is to create a new table with the content of your query, then to rename the old table, and the new table to the original table's name, to check if everything is all right, and to drop the original table if yes.
CREATE TABLE indata_new AS
WITH grp AS (
SELECT
MIN(rowid) AS orowid
, pid
, name
, MAX(date) AS date
, code
, SUM(qty) AS qty
FROM indata
GROUP BY
pid
, name
, code
)
SELECT
ROW_NUMBER() OVER(ORDER BY orowid ASC) AS rowid
, *
FROM grp;
ALTER TABLE indata RENAME TO indata_old;
ALTER TABLE indata_new RENAME TO indata;
-- if "indata" now contains the data you want ...
SELECT * FROM indata;
-- out rowid | orowid | pid | name | date | code | qty
-- out -------+--------+-----+--------+------------+------+-----
-- out 1 | 1 | 1 | bob | 2020-09-29 | 123 | 1
-- out 2 | 2 | 1 | bob | 2020-08-10 | 456 | 1
-- out 3 | 3 | 2 | rob | 2020-09-15 | 123 | 5
-- out 4 | 8 | 3 | john | 2020-07-12 | 987 | 2
-- out 5 | 10 | 4 | george | 2020-09-12 | 684 | 1
-- out 6 | 11 | 5 | paul | 2020-02-02 | 454 | 1
-- out 7 | 12 | 6 | amy | 2020-01-12 | 252 | 1
-- out 8 | 13 | 7 | susan | 2020-05-30 | 131 | 2
-- out 9 | 14 | 7 | susan | 2020-06-06 | 252 | 1
-- you can ...
DROP TABLE indata_old;
The Problem:
Construct the SQL statement to find the number of attendees for every meeting. Display the following columns:
Count of meeting attendees
Meeting ID
Meeting start date and time
Meeting end date and time
There are 5 tables in this database(person, building, room, meeting, person_meeting
+-----------+------------+------------+
| person_id | first_name | last_name |
+-----------+------------+------------+
| 1 | Tom | Hanks |
| 2 | Anne | Hathaway |
| 3 | Tom | Cruise |
| 4 | Meryl | Streep |
| 5 | Chris | Pratt |
| 6 | Halle | Berry |
| 7 | Robert | De Niro |
| 8 | Julia | Roberts |
| 9 | Denzel | Washington |
| 10 | Melissa | McCarthy |
+-----------+------------+------------+
+-------------+----------------------+
| building_id | building_name |
+-------------+----------------------+
| 1 | Headquarters |
| 2 | Main Street Buidling |
+-------------+----------------------+
+---------+-------------+-------------+----------+
| room_id | room_number | building_id | capacity |
+---------+-------------+-------------+----------+
| 1 | 100 | 1 | 5 |
| 2 | 200 | 1 | 4 |
| 3 | 300 | 1 | 10 |
| 4 | 10 | 2 | 4 |
| 5 | 20 | 2 | 4 |
+---------+-------------+-------------+----------+
+------------+---------+---------------------+---------------------+
| meeting_id | room_id | meeting_start | meeting_end |
+------------+---------+---------------------+---------------------+
| 1 | 1 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
| 2 | 1 | 2016-12-25 10:00:00 | 2016-12-25 12:00:00 |
| 3 | 1 | 2016-12-25 11:00:00 | 2016-12-25 12:00:00 |
| 4 | 2 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
| 5 | 4 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
| 6 | 5 | 2016-12-25 14:00:00 | 2016-12-25 16:00:00 |
+------------+---------+---------------------+---------------------+
+-----------+------------+
| person_id | meeting_id |
+-----------+------------+
| 1 | 1 |
| 10 | 1 |
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 2 |
| 5 | 2 |
| 6 | 2 |
| 7 | 2 |
| 8 | 2 |
| 9 | 3 |
| 10 | 3 |
| 1 | 4 |
| 2 | 4 |
| 8 | 5 |
| 9 | 5 |
| 1 | 6 |
| 2 | 6 |
| 3 | 6 |
+-----------+------------+
My SQL statement:
SELECT Count(person_id) AS “Count of meeting attendees” ,meeting_id,meeting_start,meeting_end
FROM meeting M ,person_meeting PM
WHERE M. meeting_id=PM. meeting_id
Group by PM.meeting_id,M.meeting_start,M.meeting_end;
The error I get:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'of meeting attendees” ,meeting_id,meeting_start,meet
ing_end
FROM meeting M ' at line 1
Please Help me, I am not sure What I am doing wrong. I have not been able to find a solution for this problem
Never use commas in the FROM clause. Always use proper, explicit, standard JOIN syntax.
Don't give you columns aliases that need to be escaped. So, use underscores rather than spaces. This fixes your problem with the weird double quote characters:
SELECT COUNT(pm.person_id) AS num_attendees,
m.meeting_id, m.meeting_start, m.meeting_end
FROM meeting m JOIN
person_meeting PM
ON m.meeting_id = pm.meeting_id
GROUP BY m.meeting_id, m.meeting_start, m.meeting_end ;
Based on the error text I would try changing the name of your COUNT() column to just 'Count', since the error message starts just after the first space in your column name
EDIT:
I will also second Gordon Linoff's statement:
never do this:
FROM tbla a, tblb b
WHERE a.columna = b.columnb
That's a regular old join, and it should be written as such:
FROM tbla A
JOIN tblb B ON a.columna = b.columnb
There are 2 things that can be changed. First meeting_id column belongs to both table so you have to define which table's column. And then the column naming must have error depending on error description you have provided.
SELECT Count(person_id) AS "Count" ,M.meeting_id,meeting_start,meeting_end
FROM meeting M
INNER JOIN person_meeting PM ON M.meeting_id=PM.meeting_id
Group by M.meeting_id,M.meeting_start,M.meeting_end;
The Problem:
Construct the SQL statement to find all of the people that have meetings only before Dec. 25, 2016 at noon using INNER JOINs. Display the following columns:
Person’s first name
Person’s last name
Meeting ID
Meeting start date and time
Meeting end date and time
The Tables:
There are 5 tables in this database(person, building, room, meeting, person_meeting
+-----------+------------+------------+
| person_id | first_name | last_name |
+-----------+------------+------------+
| 1 | Tom | Hanks |
| 2 | Anne | Hathaway |
| 3 | Tom | Cruise |
| 4 | Meryl | Streep |
| 5 | Chris | Pratt |
| 6 | Halle | Berry |
| 7 | Robert | De Niro |
| 8 | Julia | Roberts |
| 9 | Denzel | Washington |
| 10 | Melissa | McCarthy |
+-----------+------------+------------+
+-------------+----------------------+
| building_id | building_name |
+-------------+----------------------+
| 1 | Headquarters |
| 2 | Main Street Buidling |
+-------------+----------------------+
+---------+-------------+-------------+----------+
| room_id | room_number | building_id | capacity |
+---------+-------------+-------------+----------+
| 1 | 100 | 1 | 5 |
| 2 | 200 | 1 | 4 |
| 3 | 300 | 1 | 10 |
| 4 | 10 | 2 | 4 |
| 5 | 20 | 2 | 4 |
+---------+-------------+-------------+----------+
+------------+---------+---------------------+---------------------+
| meeting_id | room_id | meeting_start | meeting_end |
+------------+---------+---------------------+---------------------+
| 1 | 1 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
| 2 | 1 | 2016-12-25 10:00:00 | 2016-12-25 12:00:00 |
| 3 | 1 | 2016-12-25 11:00:00 | 2016-12-25 12:00:00 |
| 4 | 2 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
| 5 | 4 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
| 6 | 5 | 2016-12-25 14:00:00 | 2016-12-25 16:00:00 |
+------------+---------+---------------------+---------------------+
+-----------+------------+
| person_id | meeting_id |
+-----------+------------+
| 1 | 1 |
| 10 | 1 |
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 2 |
| 5 | 2 |
| 6 | 2 |
| 7 | 2 |
| 8 | 2 |
| 9 | 3 |
| 10 | 3 |
| 1 | 4 |
| 2 | 4 |
| 8 | 5 |
| 9 | 5 |
| 1 | 6 |
| 2 | 6 |
| 3 | 6 |
+-----------+------------+
My Solution so Far:
SELECT first_name,last_name ,building_name,meeting_start,meeting_end
FROM person P
INNER JOIN building B
ON P.person_id=PM.person_id
INNER JOIN person_meeting PM
ON M.room_id
I'm having trouble completing the SQL statement, please help if possible.
this might do the trick for you. I used aliases for each of the tables in the join and used them in the select statements for the columns you needed. Then I joined the needed tables and in the where it determines the ones with a meeting_end before dec 12 at noon. (i assume, if you wanted it start just switch it to meeting_start)
select p.first_name,p.last_name,pm.meeting_id,m.meeting_start,m.meeting_end from person p
inner join person_meeting pm on pm.person_id = p.person_id
inner join meeting m on m.meeting_id = pm.meeting_id
where m.meeting_end > '2016-12-25 12:00:00'
My table is as follow:
-------------------------------------------
| rec_id | A_id | B_id |Date(YYYY-MM-DD)|
-------------------------------------------
| 1 | 1 | 6 | 2014-01-01 |
| 2 | 5 | 1 | 2014-01-02 |
| 3 | 2 | 6 | 2015-01-03 |
| 4 | 6 | 1 | 2014-01-04 |
| 5 | 7 | 1 | 2014-01-05 |
| 6 | 3 | 6 | 2014-01-06 |
| 7 | 8 | 1 | 2014-01-07 |
| 8 | 4 | 6 | 2014-01-08 |
| 9 | 9 | 1 | 2014-01-09 |
| 10 | 10 | 21 | 2014-01-10 |
| 11 | 12 | 21 | 2014-01-11 |
| 12 | 11 | 2 | 2014-01-12 |
| 13 | 1 | 1 | 2014-12-31 |
| 14 | 2 | 2 | 2014-12-31 |
| 15 | 1 | 1 | 2015-01-31 |
| 16 | 10 | 21 | 2015-01-31 |
| 17 | 1 | 21 | 2014-10-31 |
This table represents the possession of various "A_id" to a specific "B_id" with a date when it is possessed. The possession of each "A_id" can be changed later on at any time. That means the only the latest possession is considered.
I want to find out all the "A_id" that are currently (possessed in latest date) in possession of a specific "B_id". For example, for "B_id" = 6 the possessed "A_id" at present are as follows:
---------------------------
| A_id | Date(YYYY-MM-DD) |
---------------------------
| 2 | 2015-01-03 |
| 3 | 2014-01-06 |
| 4 | 2014-01-08 |
Similarly, for "B_id" = 21 the possessed "A_id" at present are as follows:
---------------------------
| A_id | Date(YYYY-MM-DD) |
---------------------------
| 10 | 2015-01-31 |
| 12 | 2014-01-11 |
I would highly appreciate your kind help in this regard.
One way to accomplish this is to use a correlated not exists predicate that makes sure that there doesn't exists any later possession for each A_ID with another B_ID.
SELECT A_ID, MAX(PDATE) AS DATE
FROM YOUR_TABLE T
WHERE B_ID = 6
AND NOT EXISTS (
SELECT 1
FROM YOUR_TABLE
WHERE A_ID = T.A_ID
AND PDATE > T.PDATE
AND B_ID <> T.B_ID
)
GROUP BY A_ID
I'm new here, someone would have a possible solution to a problem I could not solve with subquery, any idea how to solve the problem?
Basically I need all patients "pa_name", most current exam for each "field: pe_d2" Like "Expected Result:"
I tried to make a sketch of the result, might help understand the problem ...
The "pacient_exams" table has very many records, the query needs to be very fast.
Thanks in advance for possible solutions! []
patient_exams
+-------+----------+----------+------------+------------+
| pe_id | pe_pa_id | pe_ex_id | pe_d1 | pe_d2 |
+-------+----------+----------+------------+------------+
| 1 | 1 | 1 | 2014-05-19 | 2016-05-19 |
| 2 | 1 | 2 | 2014-05-19 | 2015-05-19 |
| 3 | 1 | 3 | 2014-05-26 | 2014-11-26 |
| 4 | 1 | 3 | 2014-05-19 | 2014-11-19 |
| 5 | 1 | 4 | 2013-05-19 | 2013-11-19 |
| 6 | 1 | 4 | 2014-05-19 | 2014-11-19 |
| 7 | 3 | 1 | 2013-08-19 | 2014-08-19 |
| 8 | 3 | 1 | 2014-05-01 | 2017-05-01 |
| 9 | 4 | 2 | 2013-05-02 | 2014-05-02 |
| 10 | 4 | 2 | 2013-11-01 | 2014-05-01 |
| 11 | 4 | 4 | 2013-05-02 | 2014-05-02 |
| 12 | 4 | 4 | 2013-11-01 | 2014-05-01 |
+-------+----------+----------+------------+------------+
patient exams
+-------+---------+ +-------+---------+
| pa_id | pa_name | | ex_id | ex_name |
+-------+---------+ +-------+---------+
| 1 | John M. | | 1 | Exam 1 |
| 2 | Slater | | 2 | Exam 2 |
| 3 | Jonny | | 3 | Exam 3 |
| 4 | Jessy | | 4 | Exam 4 |
| ... | ... | | ... | ... |
+-------+---------+ +-------+---------+
Expected Result:
+-------+---------+---------+------------+------------+
| pe_id | pa_name | ex_name | pe_d1 | pe_d2 |
+-------+---------+---------+------------+------------+
| 9 | Jessy | Exam 2 | 2013-05-02 | 2014-05-02 |
| 11 | Jessy | Exam 4 | 2013-05-02 | 2014-05-02 |
| 1 | John M. | Exam 1 | 2014-05-19 | 2016-05-19 |
| 2 | John M. | Exam 2 | 2014-05-19 | 2015-05-19 |
| 3 | John M. | Exam 3 | 2014-05-26 | 2014-11-26 |
| 6 | John M. | Exam 4 | 2014-05-26 | 2014-11-26 |
| 8 | Jonny | Exam 1 | 2014-05-01 | 2017-05-01 |
+-------+---------+---------+------------+------------+
You need to first get the latest records from the patient_exams table and then join all the 3 tables with the filtered results, like this:
SELECT pe_id, pa_name, ex_name, pe_d1, pe_d2
FROM patient_exams pe
JOIN patient p
ON pe.pe_pa_id = p.pa_id
JOIN exams e
ON pe.pe_ex_id = e.ex_id
JOIN (
SELECT pe_pa_id, pe_ex_id, MAX(pe_d2) AS max_pe_d2
FROM patient_exams
GROUP BY pe_pa_id, pe_ex_id
) AS t
ON pe.pe_pa_id = t.pe_pa_id
AND pe.pe_ex_id = t.pe_ex_id
AND pe.pe_d2 = t.max_pe_d2
ORDER BY pa_name, ex_name
Demo/Solution
Thanks to everyone, works fine!
You can use joins among your tables,for the max exam date you need an additional self join to patient_exams with a subquery to get the maxima of exam date i.e max(pe_d2)
select
pe.pe_id,
p.pa_name ,
e.ex_name ,
pe.pe_d1 ,
pe.pe_d2
from exams e
join patient_exams pe on(e.ex_id = pe.pe_ex_id)
join patient p on(p.pa_id= pe.pe_pa_id)
join (select `pe_pa_id`, `pe_ex_id` ,max(pe_d2) pe_d2
from patient_exams
group by `pe_pa_id`, `pe_ex_id`) pee
on (pe.`pe_pa_id`= pee.`pe_pa_id` and
pe.`pe_ex_id` = pee.`pe_ex_id` and
pe.pe_d2 = pee.pe_d2
)
order by p.pa_name ,pee.pe_d2 desc
Demo