G
G
GolDenOne2013-05-10 22:13:07
MySQL
GolDenOne, 2013-05-10 22:13:07

MySQL query from two tables

There are two tables:
games (id, name, image) table with games
uploaded_games (id, user_id, game_id) table with games uploaded by user with user_id

You need to select from the games table only those games that are not in the uploaded_games table .

I did this:
SELECT * FROM games
LEFT JOIN uploaded_games
ON games.id != uploaded_games.game_id AND uploaded_games.user_id != 1 The

request doesn't work (empty request), please point it in the right direction.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey, 2013-05-11
@seriyPS

I hope you know this picture.

Let's say table A is SELECT * FROM games, and table B is SELECT * FROM uploaded_games WHERE user_id = ?.
Those. you need to subtract table B from table A (in terms of set theory). In the picture, this is the left column, second from the top.

SELECT *
FROM games as A
LEFT JOIN uploaded_games as B ON (A.game_id = B.game_id)
WHERE B.game_id is NULL AND B.user_id = ?

You can write it more clearly, but, most likely, MySQL does not optimize it efficiently
SELECT *
FROM games
WHERE game_id NOT IN (
  SELECT game_id FROM uploaded_games WHERE user_id = ?
)

B
bizon, 2013-05-10
@bizon

SELECT * FROM games
LEFT OUTER JOIN uploaded_games
ON games.id = uploaded_games.game_id where uploaded_games.id is null

S
sumjohn, 2013-05-10
@sumjohn

SELECT * FROM games WHERE id NOT IN (SELECT game_id FROM uploaded_games WHERE 1)

T
theaspin, 2013-05-10
@theaspin

Not quite a correct question, what does user_id have to do with it if you want to select from the games table only those games that are not in the uploaded_games table?
SELECT * FROM games g LEFT JOIN uploaded_games ug ON g.id=ug.game_id WHERE ug.game_id IS NULL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question