I have two tables like this:-
Tablea
-------
id|Comp_name|
1 |abc |
Tableb
--------
ids|id|Comp_name|
2 |1 |def|
I'm trying to inner join these two table into one with query like this
SELECT * FROM tablea
INNER JOIN tableb ON tablea.id=tableb.id
The result is like this
id|Comp_name|ids|Comp_name|
1 |abc |2 |def|
. How To separate Comp_name value into php ? I tried code like this but fail:-
foreach($query->result() as $row){
echo '<tr class="'.$class.'">
<td>
'.$row->Tablea.Comp_name.'
</td>
<td>
'.$row->Tablea.Comp_name.'
</td>
An alias can be used in a query select list to give a column a different name.
For more info Click here.
SQL Query looks like
SELECT tablea.id,tablea.Comp_name AS compname_a, tableb.*
FROM tablea
INNER JOIN tableb ON tablea.id=tableb.id
PHP
<?php foreach($query->result() as $row):?>
<tr class="<?php echo $class?>">
<td><?php echo $row->compname_a ?></td>
<td><?php echo $row->Comp_name ?></td>
</tr>
<?php endforeach;?>
You can try the following:
SELECT id, tablea.Comp_name as A_Comp_name, ids, tableb.Comp_name as B_Comp_name
FROM tablea
INNER JOIN tableb ON tablea.id=tableb.id
And the result will be something like this:
id|A_Comp_name|ids|B_Comp_name|
1 |abc |2 |def |
Use alias for this type of situation. query:
SELECT tablea.Comp_name as company1, tableb.Comp_name as company2
FROM tablea
INNER JOIN tableb ON tablea.id=tableb.id
For your desired output, you just need to use alias
for getting same column name value from two tables.
Modified Query:
SELECT tablea.id, tablea.Comp_name as FirstVal,
tableb.ids, tableb.Comp_name as SecondVal
FROM tablea
INNER JOIN tableb ON tablea.id=tableb.id
Than you can get in php something like:
$row->FirstVal // for ist value
$row->SecondVal // for second value
You can also explore more: MYSQL Alias