Jack Jack - 1 month ago 9
MySQL Question

MYSQL Query - Get and check data from 2 tables

I have a Table name USERS where I store all my users. I get the user I want by using this query:

SELECT * FROM USERS WHERE username = 'Jack'


That's working nicely.

I also have another table called "Activity" when I store users following's.

I now would like to be do a query where I select the user's using the first query and then check if I am already following this user from the "Activity" table.

My "Activity" table is setup like this:

id (for the current user / me)

IdOtherUser (the id of the user that I am following)

type (type which is 'follow')

I know this is possible but as I am quite new to MYSQL I haven't seemed to figure it out yet, I have also checked for other similar question but have not found any helpfull!

If you know any good answers already please let me know!

Any help is much appreciated, and thanks in advance to anyone that can help!!

Possible solution?

SELECT
*,
a.type
FROM USERS u
LEFT JOIN Activity a
ON a.idOtherUser = u.id AND a.id = 145
WHERE u.id = 86

Answer

Check join: SQL - Joins

SELECT
  u.id,
  u.username,
  a.type
FROM USERS u
LEFT OUTER JOIN Activity a
   ON u.id = a.id
WHERE a.IdOtherUser = 139

Check this one, it should work fine :) If you wonder what are these a, u, they are just aliases defined near column name.

IF Syntax:

IF(expression ,expr_true, expr_false);