expanding a mysql JOIN query in PHP
I need to alter my existing JOIN query below to also include the data from users.image correlating to the UserID of the post maker. Something like: users.image WHERE users.UserID = posts.userid I am not very good with join queries yet. How would I do this? Existing Query: $result = mysql_query("SELECT posts.* FROM listen JOIN posts ON posts.userid = listen.listenid WHERE listen.userid = '$user_id' ORDER BY DATE desc") or die(mysql_error());
Just add another JOIN clause: SELECT posts.* FROM listen JOIN posts ON (posts.userid = listen.listenid) JOIN users ON (users.UserID = posts.userid) WHERE listen.userid = '$user_id' ORDER BY DATE desc You may need to change the JOIN to a specific join such as LEFT JOIN, depending on what you're after. Btw, it is easier to see the query on multiple lines. Edit: You'll probably want to add additional items that you are selecting with your fields, such as SELECT posts.*, users.*
Add data to multiple data fields
How to destroy the APC cache in YII
.val() not capturing the textbox value when button is clicked
PHP calling in more than 1 Tweet
PHPExcel - How to apply borders to dynamic range of cells loaded from mysql data
CodeIgniter Active record update not working
Creating MySQL leaderboard
JSON_BIGINT_AS_STRING removed in php 5.5?
Calling a Model Function in View?
PHP Regex: Extracting content from piped curly braces
Merge these array values into a new array?
Compare directory contents to database column in Joomla
How to access the html contents in the php code segment within the same php file?
How to change the index name of array
New line chr in twitter feed breaks JSONP
FFmpeg failed to transcode - Error while opening encoder for output stream