patternsqlMinor
MySQL JOIN query produces wrong result
Viewed 0 times
resultqueryjoinmysqlwrongproduces
Problem
I have two tables
NB: The person who adds complaints is complaint owner & person who adds a complaint reply is complaint replier. Complaint owner can also add replies. So he can either be the complaint owner or the complaint replier. The two tables have a one-to-many relationship. A complaint can have more than one complaint reply.
DESIRED OUTPUT:
Join the two tables and fetch values and show the complaint and complaint’s reply as a single result set. But the condition is kinda tricky. The last added complaint reply from the
So, from the sample data the tables contain now, the output that I should get is:
But what I got is:
```
+------+---------+----------+-------------+-------------------+
| id | title |member_id |last_replier |last_posted_dt |
+--
complaints and complaints_reply in my MySQl database. Users can add complaints which are stored in complaints the complaints reply are stored in complaints_reply table. I am trying to JOIN both these table contents on a specific condition. Before I mention what I am trying to get and the problem I faced, I will explain the structure of these two tables first.NB: The person who adds complaints is complaint owner & person who adds a complaint reply is complaint replier. Complaint owner can also add replies. So he can either be the complaint owner or the complaint replier. The two tables have a one-to-many relationship. A complaint can have more than one complaint reply.
member_id in complaint table represents complaint owner & mem_id in complaints_reply represent complaint replierDESIRED OUTPUT:
Join the two tables and fetch values and show the complaint and complaint’s reply as a single result set. But the condition is kinda tricky. The last added complaint reply from the
complaints_reply table should be fetched for the complaint in complaints table in such a way that the complaint owner should not be the complaint replier. I use posted_date & posted_time from complaints_reply table to fetch the last added complaint reply for a complaint & that complaint replier has to be shown in the result set.So, from the sample data the tables contain now, the output that I should get is:
+------+---------+----------+-------------+-------------------+
| id | title |member_id |last_replier |last_posted_dt |
+------+---------+----------+-------------+-------------------+
| 1 | x | 1000 |2002 | 2015-05-2610:11:17|
| 2 | y | 1001 |1000 | 2015-05-2710:06:16|
+------+---------+----------+-------------+-------------------+But what I got is:
```
+------+---------+----------+-------------+-------------------+
| id | title |member_id |last_replier |last_posted_dt |
+--
Solution
The problem is the subquery, where you combine in the select list both an aggregated (
MySQL allows you to run this kind of inconsistent queries - with default settings - which basically means it depends on the developer to write consistent queries and not fire themselves on the foot. You can change the sql mode to
Now to solve the issue, it seems you want a
Here's one way:
It will give you consistent results and also be quite efficient, if you add an index on
Tested at SQLfiddle.
max(cr.posted_dt)) and non-aggregated expressions/columns from the tables. MySQL allows you to run this kind of inconsistent queries - with default settings - which basically means it depends on the developer to write consistent queries and not fire themselves on the foot. You can change the sql mode to
ONLY_FULL_GROUP_BY and see what happens if you try to run your query. Now to solve the issue, it seems you want a
[greatest-n-per-group] type of query. There are several ways to do this, all quite complicated in MySQL (because it lacks window functions). Check the relevant tags in this site and in the SO main site.Here's one way:
SELECT c.id,
c.title,
c.member_id,
cr.mem_id AS last_replier,
CONCAT(cr.posted_date, 'T', cr.posted_time) AS last_posted_dt
FROM complaints AS c
LEFT JOIN complaints_reply AS cr
ON cr.id =
( SELECT crl.id
FROM complaints_reply AS crl
WHERE crl.complaint_id = c.id
AND crl.mem_id <> c.member_id
ORDER BY posted_date DESC, posted_time DESC
LIMIT 1
) ;It will give you consistent results and also be quite efficient, if you add an index on
(complaint_id, posted_date, posted_time, mem_id)Tested at SQLfiddle.
Code Snippets
SELECT c.id,
c.title,
c.member_id,
cr.mem_id AS last_replier,
CONCAT(cr.posted_date, 'T', cr.posted_time) AS last_posted_dt
FROM complaints AS c
LEFT JOIN complaints_reply AS cr
ON cr.id =
( SELECT crl.id
FROM complaints_reply AS crl
WHERE crl.complaint_id = c.id
AND crl.mem_id <> c.member_id
ORDER BY posted_date DESC, posted_time DESC
LIMIT 1
) ;Context
StackExchange Database Administrators Q#101111, answer score: 5
Revisions (0)
No revisions yet.