Forum Moderators: open

Message Too Old, No Replies

How to use two joins on a sql query?

         

shammi007

5:29 pm on Sep 29, 2014 (gmt 0)

10+ Year Member



Sorry asking such a newbie question but I've been trying to understand this SQL query and add a thing and i can't make it happen.

Here is the query:

select
p.id, p.nick, p.creation_date
from
tb_player p
left outer join
tb_invoice i on (i.player_id = p.id), tb_player_last_login tpl
where
p.creation_date < now() - '12 months'::interval
and tpl.last_login_date < now() - '12 months'::interval
and tpl.player_id = p.id
and p.id > 9999
and (p.email = 'EDITE-SEU-EMAIL' or p.email = 'configure-seu-email')
and i.id is null
limit 15000;

So I'm selecting a group of people from this tb_player and I have another table called tb_email_list and I need to say somewhere to include the players of tb_player that have the same email of tb_email_list that are valid.

tb_player has nick and email camp
tb_email_list player_id, email, is_valid
I tried to put some joins but nothing I do seems to work...

A little help please?

My friend helped me and it goes like

select p.id, p.nick, p.creation_date
from tb_player_last_login tpl, tb_player p
left outer join tb_invoice i on (i.player_id = p.id)
left join tb_email_list e on e.player_id = p.id
where p.creation_date < now() - '12 months'::interval
and tpl.last_login_date < now() - '12 months'::interval
and tpl.player_id = p.id
and p.id > 9999
and (p.email = 'EDITE-SEU-EMAIL' or p.email = 'configure-seu-email' or e.is_valid = -1)
and i.id is null
limit 15000

GoNC

11:44 pm on Oct 5, 2014 (gmt 0)

10+ Year Member



I'm not a pro at MySQL myself, but try this:

SELECT p.id, p.nick, p.creation_date FROM tb_player AS p

.. LEFT JOIN tb_player_last_login AS tpl
... ON tpl.player_id = p.id
..... AND tpl.last_login_date < now() - '12 months'::interval

.. LEFT JOIN tb_invoice AS i
... ON i.player_id = p.id
..... AND i.id IS NULL

.. LEFT JOIN tb_email_list AS e
... ON e.player_id = p.id

. WHERE p.id > 9999
... AND p.creation_date < now() - '12 months'::interval
... AND (
..... p.email = 'EDITE-SEU-EMAIL'
..... OR p.email = 'configure-seu-email'
..... OR e.is_valid = -1
... )
LIMIT 15000

I added the .. to space it out and make it more legible.

If that doesn't work, my first step here would be to try to simplify the code as much as possible to see what's happening, then add in conditions until you find the problem. Like:

SELECT * FROM tb_player_last_login AS tpl
JOIN tb_player AS p
LEFT JOIN tb_invoice AS i ON i.player_id = p.id
LEFT JOIN tb_email_list AS e ON e.player_id = p.id
LIMIT 5

If that brings in the data you expect, then add back the first WHERE condition, and so on.

HTH!