ChrVik ChrVik - 1 year ago 134
MySQL Question

SQL - Limit information in JOIN

I have a database with 2 tables, users and news. I'm combining information from the two tables using this SQL:

$news = $db->query('SELECT * FROM news LEFT JOIN users ON users.userid = news.userid')->fetchAll();

The problem here is that I'm also dragging along information that I have no use for, like password etc. How can I limit the query to only include the name of the user from the users table?

Answer Source

If you want all columns from one table, and only selected columns from another table, you can use tablename.* to get the first, and tablename.columnname for the second.

SELECT news.*,
FROM news 
LEFT JOIN users ON users.userid = news.userid
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download