php


Selecting from the same table more than once in the same query


I have a table called exhibit which contains:
ex_id
ex_inv_id
ex_user_id
ex_pref_one
ex_pref_two
ex_pref_three
ex_pref_four
ex_terms_conditions
ex_pref_one_approved
ex_pref_two_approved
ex_pref_three_approved
ex_pref_four_approved
And another table called stand which contains:
stand_id
stand_no
stand_type
I need to put the values in a datatable now by selecting from the exhibit table where the ex_pref_one/two/three/four columns = stand_id
I tried using a join but i got an error that said the table stand does not exist
I am using the codeigniter PHP framework
Here is the Code Sorry i didnt post it first
$user_id = $this->session->userdata('user_id');
if(!$user_id || $user_id == 0 )return 0;
$where = array(
'exhibit.ex_user_id'=>$user_id,
);
return $this->db->select('
exhibit.*,
S1.*,
S2.*,
S3.*,
S4.*,
')
->join('stand AS S1', 'exhibit.ex_pref_one = S1.stand_id', 'LEFT')
->join('stand AS S2', 'exhibit.ex_pref_two = S2.stand_id', 'LEFT')
->join('stand AS S3', 'exhibit.ex_pref_three = S3.stand_id', 'LEFT')
->join('stand AS S4', 'exhibit.ex_pref_four = S4.stand_id', 'LEFT')
->where($where)
->from('exhibit')
->get()
->result();
Result of $this->db->last_query();
SELECT `exhibit`.*,
`S1`.*,
`S2`.*,
`S3`.*,
`S4`.*
FROM (`exhibit`)
LEFT JOIN `stand` AS S1 ON `exhibit`.`ex_pref_one` = `S1`.`stand_id`
LEFT JOIN `stand` AS S2 ON `exhibit`.`ex_pref_two` = `S2`.`stand_id`
LEFT JOIN `stand` AS S3 ON `exhibit`.`ex_pref_three` = `S3`.`stand_id`
LEFT JOIN `stand` AS S4 ON `exhibit`.`ex_pref_four` = `S4`.`stand_id`
WHERE `exhibit`.`ex_user_id` = 1
var_dump of the result
array (size=1)
0 =>
object(stdClass)[25]
public 'ex_id' => string '2' (length=1)
public 'ex_inv_id' => string '2147483647' (length=10)
public 'ex_user_id' => string '1' (length=1)
public 'ex_pref_one' => string '6' (length=1)
public 'ex_pref_two' => string '14' (length=2)
public 'ex_pref_three' => string '13' (length=2)
public 'ex_pref_four' => string '21' (length=2)
public 'ex_terms_conditions' => string '1' (length=1)
public 'ex_pref_one_approved' => string '0' (length=1)
public 'ex_pref_two_approved' => string '0' (length=1)
public 'ex_pref_three_approved' => string '0' (length=1)
public 'ex_pref_four_approved' => string '0' (length=1)
public 'stand_id' => string '21' (length=2)
public 'stand_no' => string '20' (length=2)
public 'stand_type' => string 'Gold' (length=4)
But it seems that it only pulls the ex_pref_four through and none of the others Why is that ?
First to alias you don't need AS. so you can do it like this:
$this->db->select('
exhibit.*,
S1.*,
S2.*,
S3.*,
S4.*,
')
->from('exhibit')
->join('stand S1', 'exhibit.ex_pref_one = S1.stand_id', 'LEFT')
->join('stand S2', 'exhibit.ex_pref_two = S2.stand_id', 'LEFT')
->join('stand S3', 'exhibit.ex_pref_three = S3.stand_id', 'LEFT')
->join('stand S4', 'exhibit.ex_pref_four = S4.stand_id', 'LEFT')
->where('whatever')
You need to give multiple joins to the same table a distinguishing reference. i.e.
SELECT * FROM exhibit LEFT JOIN stand AS a ON exhibit.ex_pref_one = stand.stand_id LEFT JOIN stand AS b ON exhibit.ex_pref_two = stand.stand_id
Then refer to the results using those references - you'll see what I mean from the results you get from such a query.

Related Links

Call to undefined method Imagick::roundCorners() on MacOSX
Is there a PHP native method that delete row of a multidimensional array?
how to fix an error in SQL syntax
How to avoid server irresponsive when a PHP script fails
Create a new Telegram bot
Multi word search doesnt show array in PHP
PayPal API - Payments Pro Required To Process Billing Methods Directly?
change user password from hash store function to cleartext
Why does my mysqli_query return false?
Opencart website shows error when uploaded to server
Test SMTP Connection through AfterLogic
Disable WP Super Cache When a Cookie is Set
Linking in wordpress, which link is right?
Apache server localhost working but not server IP
trying get post body in wordpress but not working
WordPress: Frontend form to create custom type post with featured image

Categories

HOME
mod-rewrite
keyboard
tomcat
add-on
haproxy
subdomain
message-queue
apk
mithril.js
joomla3.2
onvif
selection
erd
sslhandshakeexception
corona
texas-instruments
cocos2d-android
renderscript
tableview
fido-u2f
maven-surefire-plugin
ava
mod-wsgi
spreadsheetgear
oracleforms
basex
code-climate
progid
commonsware-cwac
memory-fences
opnet
mongoengine
winrm
gnu-classpath
bootstrap-sass
char-pointer
wdk
landscape
assertions
cheat-engine
sbjson
stat
swfupload
klee
get-event-store
ejml
contactless-smartcard
react-intl
sonarqube5.2
xcglogger
gnome-terminal
prefetch
data-management
jenkins-workflow
uibinder
exists
fdt
cortana
ajax4jsf
json-spirit
nette
screen-orientation
darwin
clarion
deque
tidyr
incron
rubber
qt5.4
lmax
webautomation
accessory
stripe.net
zend-search-lucene
bittorrent-sync
property-injection
jomsocial
vlab
progress-db
sql-view
jquery-transit
lru
ldif
dynamic-rdlc-generation
eager-loading
asp.net-mvc-3-areas
ntlmv2
icenium
towers-of-hanoi
msinfo32
uploading
zend-server-ce
rtd
bll
gaelyk
bass
mongrel
odac
.net-services
tso
autobench
acceleration

Resources

Encrypt Message