O
O
Oleg2018-09-20 16:39:20
MySQL
Oleg, 2018-09-20 16:39:20

How to make a selection from two tables?

There is a table gamefication 5ba3a35548644602163309.png
In it the list of all "tasks" for users.
There is a customer_gamefication table 5ba3a35bc2b08780762936.png
. It stores the id of users who have completed some of the tasks.
I need to display all the data from the gamefication table (All tasks) and enter matches with customer_gamefication for a specific user (customer_id)
I tried to do it and everything came out correctly, but other users got into the result, although I specified WHERE customer_id
5ba3a286b49ed953408777.jpeg

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Oleg, 2018-09-20
@bysobi

I found a solution

SELECT 
g.id, g.step_id, g.name, cg.customer_id, cg.status, cg.date_added FROM oc_gamefication g
LEFT JOIN oc_customer_gamefication cg ON (g.id = cg.gamefication_id AND cg.customer_id = 26854)

A
Alex-1917, 2018-09-20
@alex-1917

And why are you afraid of a flat request???

SELECT g.step_id, g.name, cg.status, cg.date_added 
FROM oc_gamefication g, oc_customer_gamefication cg 
WHERE g.customer.id = 26854 AND g.id = cg.gamefication_id

N
nozzy, 2018-09-20
@nozzy

select t1.*, t2.*
from oc_gamefication t1 
inner join oc_customer_gamefication t2 on t2.gamefication_id = t1.id
where t1.customer_id = 11111

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question