Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql Join a View Table as a Boolean

I have a users table, and a view table which lists some user ids... They look something like this:

Users:

User_ID  |   Name       |   Age   | ...
   555      John Doe        35
   556      Jane Doe        24
   557      John Smith      18

View_Table

User_ID
  555
  557

Now, when I do run a query to retrieve a user:

SELECT User_ID,Name,Age FROM Users WHERE User_ID = 555

SELECT User_ID,Name,Age FROM Users WHERE User_ID = 556

I also would like to select a boolean, stating whether or not the user I'm retrieving is present in the View_Table.

Result:

   User_ID           Name          Age      In_View
    555             John Doe       35         1
    556             Jane Doe       24         0

Any help would be greatly appreciated. Efficiency is a huge plus. Thanks!!

like image 780
pws5068 Avatar asked Dec 06 '22 02:12

pws5068


1 Answers

SELECT Users.User_ID,Name,Age, View_Table.User_ID IS NOT NULL AS In_View
FROM Users 
LEFT JOIN View_table USING (User_ID)
WHERE User_ID = 555
like image 93
Naktibalda Avatar answered Dec 10 '22 09:12

Naktibalda