0

I am getting some unexpected results from a SQL query.

Table data:

users:
id   username
1    admin
2    x1
3    y1
4    z1

my_connections:
id user_id friend_user_id status
1  1       2              201
2  2       1              201
3  2       4              201
4  1       3              200
5  2       3              201
6  3       2              201
7  4       2              201
8  4       1              200

jobs:
id user_id company_name designation from_date     to_date
1  1       A            1           2011-06-01    2011-07-30
2  1       B            11          2011-08-02    2014-01-20
3  2       c            12          2012-05-02    2014-01-20
4  3       D            13          2010-05-02    2014-01-20
5  4       E            11          2009-05-25    2014-01-01

Here is my query:

SELECT users.id,users.username,my_connections.user_id,my_connections.friend_user_id,my_connections.status,jobs.user_id,jobs.company_name,
jobs.designation,jobs.from_date,MAX(jobs.to_date)
FROM users
LEFT JOIN jobs ON jobs.user_id = users.id
LEFT JOIN my_connections ON my_connections.friend_user_id = users.id
WHERE my_connections.status = 201  AND users.id IN (1,3,4) 
GROUP BY jobs.company_name
ORDER BY jobs.to_date DESC

And the results:

id username user_id friend_user_id status user_id company_name designs from_date to_date                   
3    ..       2         3             201       3      D           ..  2010-05-02 2014-01-20                          
4    ..       2         4             201       4      E           ..  2009-05-25 2014-01-01                                
1    ..       2         1             201       1      A           ..  2011-08-02 2014-01-20                               
1    ..       2         1             201       1      B           ..  2011-06-01 2011-07-30

In the result, I wanted one row per friend_user_id, with the maximum value of to_date. Instead I am getting multiple rows (if there are multiple rows in the jobs table).

How can I fix this query?

Elliot Nelson
  • 11,371
  • 3
  • 30
  • 44

1 Answers1

0

if you want unique results on the friend_user_id field you must group by friend_user_id. This will guarantee unique results on the friend_user_id column. But im pretty sure you don't want that because it may show incorrect data. I am still unsure how the query is working because the group by only contains one field. You must group by all the raw fields in the select query and perform aggregate functions on fields that are not in the group by clause for example:

SELECT users.id,users.username,my_connections.user_id,my_connections.friend_user_id,my_connections.status,jobs.user_id,jobs.company_name,
jobs.designation,jobs.from_date,MAX(jobs.to_date)
FROM users
LEFT JOIN jobs ON jobs.user_id = users.id
LEFT JOIN my_connections ON my_connections.friend_user_id = users.id
WHERE my_connections.status = 201  AND users.id IN (1,3,4) 
GROUP BY users.id,users.username,my_connections.user_id,my_connections.friend_user_id,my_connections.status,jobs.user_id,jobs.company_name,
jobs.designation,jobs.from_date
ORDER BY jobs.to_date DESC

In this query all of the fields in the group by clause are in the select clause. Now all the fields not included in the group by clause can use functions like: MAX(), AVG(), SUM() etc.

Luke101
  • 63,072
  • 85
  • 231
  • 359