php


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.*

Related Links

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

Categories

HOME
class
azure-functions
bots
vsts-build
gallery
facebook-android-sdk
ag-grid
save
angular2-template
mailing-list
reportviewer
yahoo
spring-security-oauth2
parsley.js
vertica
checkout
codefluent
spring-shell
spyder
android-permissions
workday
bobo-browse.net
cube
tilemill
locks
dreamfactory
jslint
strapi
stat
game-center
slideshow
grails3.2.0
icepdf
dagger
minikube
dbcc
destroy
dredd
sonarqube5.2
return-type
roracle
karabiner
punctuation
sequence-diagram
test-data
mobile-angular-ui
dex
apache-modules
degrees
perl-data-structures
portal
elastic4s
java-2d
dmarc
dojo-build
trust
grinder
in-app-billing
variable-length-array
fantom
karma-coverage
textscan
phpredis
rfc5545
kognitio-wx2
lifetime
password-recovery
inequality
ecos
angular-ui-select
mfmessagecomposeview
hyperloglog
commenting
cloo
vlab
boinc
notifyjs
stty
session-0-isolation
red-system
dojo-1.9
gprof
lynx
system-information
uitextfielddelegate
jzmq
trialware
platform-independent
uipangesturerecognizer
prototypal-inheritance
fitch-proofs
pyjamas
turbine
bucket
powergui
wmd-editor
jquery-effects
objectinstantiation

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App