Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql query join/inner join

i have two tables in my mysql i want to extract the results based on combined query for both tables. i tried join as well as inner join but no success the structure of

tableA is

id   userid   topic

1     34       love
3     64       friendship
35    574      romance
32    253      games
95    633      football
54    26       cricket
648    63      music

tableB is

id    location     username
34      Australia    krkrff
64      india        dieiei
574     pakistan     frkfrf
253     japan        frfffrk
633     india        ifirf
26      Australia    riiri
63      Australia    frffjrr

Please note that in tableA userid and in TableB id is same .both reflect the data of same users.i want to display tableA data by filtering location column from tableB. suppose that i want to display topic of tableB and the users belongs to Australia then it should give output :love cricket music

you can see in tableB that 34,26 & 63 belongs to Australia so the output is like that. if the location is india then outpput will be

friendship and football.please tell how to write sql query.

like image 762
Steeve Avatar asked Dec 21 '22 23:12

Steeve


1 Answers

The following should select what you're describing:

select a.topic
from tableA a
join tableB b on b.id = a.userid
where b.location = 'Australia' -- or whichever location you filter on

which is equivalent to:

select a.topic
from tableA a
join tableB b on b.id = a.userid and b.location = 'Australia'
like image 151
Paul Bellora Avatar answered Dec 31 '22 11:12

Paul Bellora