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

mocking out database queries laravel mockery
Apache has just stopped working in XAMPP, new version says UAC issues
ecplode and search in rows
How to get the name of a key in array
Database not Updating Values
Integrity constraint violation using factory relationships in laravel 5.2
how to Save image in sever folder
Mysql update all columns starting with same Name
understanding the ternary operator in php
CSV with fputcsv() third parameter
PHP OOP Login system - member function getUsername() returns NULL
How to make a query to view a specific ID/ 1 line
Facebook access_token automatic renewal
Php, isset - default value, can it be simplified?
Converting MySQL PHP code / query to DB2 [closed]
how to export to excel file with multiple sheets wherein data is from database using mysqli

Categories

HOME
flask
orientdb
mc
plesk
d3.js
swi-prolog
activex
kivy
stacktrace.js
bing-search
cypher
iptables
azure-data-lake
visualforce
haxe
angular-meteor
scheduler
sap-fiori
chromecast
c#-7.0
vertica
adobe-illustrator
jest
pe
cell
firemonkey-style
beego
fusion
series
actframework
gsp
pdfminer
django-smart-selects
game-center
newtons-method
access-denied
vesta
spongycastle
aws-kinesis-firehose
nothing
node-apn
maven-jaxb2-plugin
watir-webdriver
google-news
scala-collections
deedle
emacs25
wildfly-9
ternary-operator
cefpython
nsrunloop
yii2-model
uibinder
facebook-ios-sdk
preferenceactivity
ioctl
diawi
boost-proto
gitlab-omnibus
wlanapi
postal-code
artisan
physicsjs
tree-structure
azure-xplat-cli
thoughtworks-go
osascript
webmethod
rails-api
wso2bam
jomsocial
hints
sunstudio
mongo-jackson-mapper
red-system
dojo-1.9
handshake
idispatch
stretch
audiotoolbox
popup-blocker
quartz-core
sigar
delphi-prism
server-variables
kernel32
upshot
ognl
formal-semantics
ios-4.2
ixmldomdocument
getimagesize
fuzzy-comparison
acceleration
regioninfo
html-generation

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App