Welcome Guest, Not a member yet? Register   Sign In
mysql join problem
#1

(This post was last modified: 08-18-2021, 12:51 AM by InsiteFX. Edit Reason: Fixed code tags please use them )

My join is inserting the foreign key in place of the primary key.

PHP Code:
$builder $this->db->table('elongate');
$builder->select('elongate.id, elongate.catalog_num, elongate.city_id, elongate.venue_id, city.id, city.city_name');
$builder->join('city''city.id = elongate.city_id');

The result is:
Array
(
    [0] => Array
        (
            [id] => 1
            
[catalog_num] => OR-ALB-HC-01
            
[city_id] => 1
            
[venue_id] => 32
            
[city_name] => Albany
        
)

    [1] => Array
        (
            [id] => 1
            
[catalog_num] => OR-ALB-HC-02
            
[city_id] => 1
            
[venue_id] => 32
            
[city_name] => Albany
        
)

    [2] => Array
        (
            [id] => 1
            
[catalog_num] => OR-ALB-HC-03
            
[city_id] => 1
            
[venue_id] => 32
            
[city_name] => Albany
        
)

    [3] => Array
        (
            [id] => 1
            
[catalog_num] => OR-ALB-HC-04
            
[city_id] => 1
            
[venue_id] => 32
            
[city_name] => Albany 


But the [id] should be 1,2,3,4 respectively
Reply
#2

@magiwells ,

Maybe in your join statement you should specify which join you wish to use... Options are: left, right, outer, inner, left outer, and right outer.

Your statement:
$builder->join('city', 'city.id = elongate.city_id');

Try (one of the options listed above. I chose left for this example):
$builder->join('city', 'city.id = elongate.city_id', 'left');
Reply
#3

(08-17-2021, 07:26 AM)php_rocs Wrote: @magiwells ,

Maybe in your join statement you should specify which join you wish to use... Options are: left, right, outer, inner, left outer, and right outer.

Your statement:
$builder->join('city', 'city.id = elongate.city_id');

Try (one of the options listed above.  I chose left for this example):
$builder->join('city', 'city.id = elongate.city_id', 'left');

I tried the join options before I posted and got the same result.   It's weird when I do a sql query outside of Codeigniter, I get the expected result.
There must be something Im overlooking.  I am just learning Codeigniter.
Reply
#4

Try this.
PHP Code:
$builder $this->db->table('elongate');
$builder->select('elongate.id, elongate.catalog_num, elongate.city_id, elongate.venue_id, city.id, city.city_name');
$builder->join('city''city.id = elongate.city_id');
$query $builder->get();

var_dump($query->getResultArray()); 

Also you can use $db->getLastQuery(); to see what your query looks like.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

try to remove city.id from the select section.
you already got the city.id as elongated.city_id

in your case elongate.id will be replaced with city.id
Reply
#6

(08-18-2021, 03:05 AM)iRedds Wrote: try to remove city.id from the select section.
you already got the city.id as elongated.city_id

in your case elongate.id will be replaced with city.id

This was it 
Thank you
Reply
#7

@magiwells ,

Also, if you still want city.id as a part of your results you could do this...

$builder->select('elongate.id, elongate.catalog_num, elongate.city_id, elongate.venue_id, city.id as city_id, city.city_name');

Then city.id would be called city_id instead of id.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB