Forum Moderators: open

Message Too Old, No Replies

Query Help Please

Trying to use inner join

         

psiron

4:04 pm on Dec 3, 2007 (gmt 0)

10+ Year Member



Hi,

I am a novice to PHP and seem to be scraping through my code constantly. I am getting unpredictable results at times with my queries so I am trying to use a join if possible.

Can somone please tell me what the best way to query the following:

I have 3 tables, each table has some info that I need:

tbl_messages has the messageID
tbl_clients has the clients details
tbl_login has the clients sub-client id

My current query is:

$userid= $_SESSION_VARS['iuid'];

SELECT *
FROM tbl_messages, tbl_clients, tbl_login
WHERE msgto = 'userid' AND tbl_clients.clientid = tbl_messages.msgfrom AND tbl_messages.msgfrom = tbl_login.loginid

But I'm sure it's not the best way of doing this?

Any help would be much appreciated, I tried the 'join' command but struggled joining the tables.

Thanks in advance,

JJ

syber

5:16 am on Dec 5, 2007 (gmt 0)

10+ Year Member



The JOIN command is cleaner once you get used to it. In your example it would be:

SELECT *
FROM tbl_messages JOIN tbl_clients
ON tbl_clients.clientid = tbl_messages.msgfrom
JOIN tbl_login
ON tbl_messages.msgfrom = tbl_login.loginid
WHERE msgto = 'userid'