mysql - Put value in custom defined column in sql query -


i create sql query create column doesn't exists in db tables , gets fills on whether row exists or not exists in specific table.

for example: have 3 tables:
users (for users list) - uid, uname
locations (list of available locations) - lid, lname
userslocations (all locations users have checked into) - userid, locationid
need sql query user id me table of locations column says whether user has been in location or not.

example users table

uid | uname 1     john 4     amy 5     dann 

example locations table:

lid | lname 1     london 2     barcelona 3     paris 4     new york 

example userslocations table:

userid | locationid     5        1     5        2 

example output (for userid = 5):

user id | location | here 5         london     true 5         barcelona  true 5         paris      false 5         new york   false 

the output needs include locations locations table. userslocations table contains userid of users checked location.

hmmm. 1 method correlated subquery:

select u.userid, l.location,        (case when exists (select 1 userlocations ul.userid = u.userid , ul.lid = l.locationid)              'true'              else 'false'         end) washere         location l cross join      (select 5 userid) u; 

the part database-specific subquery u.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -