how to display matching values from different tables which used in joins without duplicates

All we need is an easy explanation of the problem, so here it is.

Here, I wrote a query using joins, the relation is fine, but I wanted display multiple columns from different tables which I used in joins, when I am trying to include or select other table columns it is performing cross join. Getting confused how to do add those columns which matches the conditions in joins. Can anyone please help me on this

select distinct p.*,ps.*,pp.*,pd.VERIFICATION_SUBJECT_ID,pd.VALUE
from Players p 
join Players_Service ps on p.ID=ps.ID
join Players_Data pd on pd.VERIFICATION_SUBJECT_ID=ps.VERIFICATION_SUBJECT_ID
join Players_Property pp on pp.ID=ps.ID

Players table:

[{ID:1,NAME:'abc'}]

Players_Data table:

[{VERIFICATION_SUBJECT_ID:3, VALUE:'AB-TYS'},{VERIFICATION_SUBJECT_ID:4, VALUE:'AB-QE'}]

Players_Property:

[{ID:1,CITY:'XYZ',ID:2,CITY:'ABC'}]

Player_Service:

[{VERIFICATION_SUBJECT_ID:3,PLACE:'ABCDE'}]

Expected Output:

ID:1,
NAME:'abc',
VERIFICATION_SUBJECT_ID:3,
PLACE:'ABCDE',
CITY:'XYZ',
VALUE:'AB-TYS'

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Instead of using ".*", spell out the columns you desire:

SELECT p.ID,
       p.NAME,
       pd.VERIFICATION_SUBJECT_ID,
       ...

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply