0

i have following sql in java project:

select distinct * from drivers inner join licenses on  drivers.user_id=licenses.issuer_id
        inner join users on drivers.user_id=users.id 
        where (licenses.state='ISSUED' or drivers.status='WAITING')
        and users.is_deleted=false 

And result i database looks like this: enter image description here

And i would like to get only one result instead of two duplicated results. How can i do that?

3
  • 1
    Which one do you want? Commented Apr 19, 2021 at 8:53
  • 1
    Sample data is better presented as formatted text. See here for some tips on how to create nice looking tables. Commented Apr 19, 2021 at 8:53
  • You need to provide sample data, desired results, and a clear explanation of what you want to accomplish. Commented Apr 19, 2021 at 11:16

4 Answers 4

1

Solution 1 - That's Because one of data has duplicate value write distinct keyword with only column you want like this

Select distinct id, distinct  creation_date, distinct  modification_date from 
YourTable

Solution 2 - apply distinct only on ID and once you get id you can get all data using in query

select * from yourtable where id in (select distinct id from drivers inner join 
 licenses 
 on drivers.user_id=licenses.issuer_id
 inner join users on drivers.user_id=users.id 
 where (licenses.state='ISSUED' or drivers.status='WAITING')
 and users.is_deleted=false )
Sign up to request clarification or add additional context in comments.

Comments

0

Enum fields name on select, using COALESCE for fields which value is null.

Comments

0

usually you dont query distinct with * (all columns), because it means if one column has the same value but the rest isn't, it will be treated as a different rows. so you have to distinct only the column you want to, then get the data

1 Comment

ok so it is not possible to do that in only one sql right? "so you have to distinct only the column you want to, then get the data"- yes i can distincst only one column and i will receive id i need, but i would like to get all data(not only id)
0

I suspect that you want left joins like this:

select *
from users u left join
     drivers d
     on d.user_id = u.id and d.status = 'WAITING' left join
     licenses l
     on d.user_id = l.issuer_id and l.state = 'ISSUED'
where u.is_deleted = false and
      (d.user_id is not null or l.issuer_id is not null);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.