Jump to content

[SOLVED] Easier way to call a row from multiple tables.


briant

Recommended Posts

database structure

 

users

-- id

-- name

 

votes

-- id

-- user_id

 

--------------------

 

if i have only the votes[id] and would like to display the users[name] i would do...

 

$voteuserid = SELECT user_id FROM votes WHERE id LIKE '$id';

 

and then...

 

$voteusername = SELECT name FROM users WHERE id LIKE '$voteuserid';

 

--------------------

 

instead of clustering them inside of one another, is there an easier more direct way?

Have you tried it?

 

When you left join you have to join on common fields, such as 'id' and specify what tables each field comes from.

 

Why are you using LIKE?  I will leave it that way, but it seems weird.

 

Try this:

 

$sql = "SELECT u.name FROM user u LEFT JOIN votes v ON u.id = v.user_id WHERE u.id LIKE '$id' AND v.id LIKE '$voteuserid'";

 

 

 

I read the tutorial of it on w3schools.com since it looks a lot easier to follow than the link you provided.

 

It was exactly what I was looking for, thanks so much Maq.

 

I guess you can do LIKE or equal. It works either way. I always thought those expressions should only deal with numbers.

You should use '='.  Like is used to match things that are 'like' what you're comparing to, not exact.

 

For example you could use the wildcard '%'.

 

SELECT * FROM table WHERE userid LIKE '1%'

 

This will match anything that begins with a 1 (12, 13, 14 etc...)

 

Hope this helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.