...

View Full Version : Field Linking



Dhuan
03-30-2009, 12:20 AM
Awright, there's this table called 'users':

|user_id|name|password|

Ok, now, there's the table 'news':

|news_id|user_id|news|

What I'd like to do is to link this 'user_id' with the 'user_id' from the other table. What's the best method for doing this?

Old Pedant
03-30-2009, 03:21 AM
Depends on what you want to do with the result.

If all you want to do is get all the related fields from both tables:


SELECT U.user_id, U.name, U.`password`,N.news_id,N.news
FROM `users` AS U, news AS N
WHERE U.user_id = N.user_id
ORDER BY U.name, N.news_id

If you need *all* users, even if they don't HAVE any records in the news table, you have to use a LEFT JOIN:


SELECT U.user_id, U.name, U.`password`,N.news_id,N.news
FROM `users` AS U LEFT JOIN news AS N ON U.user_id = N.user_id
ORDER BY U.name, N.news_id



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum