Hitesh Chauhan
Hitesh Chauhan

Reputation: 1064

How to Join two tables in codeigniter in a single column

I want to Join the two tables in CodeIgniter in a single column. For example, There is table A as shown in images

enter image description here

Table B

enter image description here

Desired Result After Joining Table A and Table B

enter image description here

Upvotes: 1

Views: 159

Answers (4)

Ravi Sharma
Ravi Sharma

Reputation: 370

select Name from TableA Union select Name from TableB

Upvotes: -1

Danish Ali
Danish Ali

Reputation: 2352

Try this

$result1 = $this->db->get('TableA');

$result2 = $this->db->get('TableB');

If you want to merge only specific columns use select()

$this->db->select('name');
$result1 = $this->db->get('TableA');

$this->db->select('name');
$result2 = $this->db->get('TableB');

And now merge these two records

$combine = array_merge($result1, $result2);

Read more about array_merge()

Upvotes: 1

M.Hemant
M.Hemant

Reputation: 2355

Try this,

$tableA = $this->db->select('name')->get_compiled_select('tableA');
echo $tableA;
$tableB = $this->db->select('name')->get_compiled_select('tableB');
echo $tableB;
$tableAB = $this->get_compiled_select($tableA.' UNION '.$tableB);
echo $tableAB;
$query = $this->db->query($tableAB);

Output:

SELECT name FROM tableA
SELECT name FROM tableB
SELECT name FROM tableA UNION SELECT name FROM tableB

OR:

$query = $this->db->query("SELECT name FROM tableA UNION SELECT name FROM tableB");

Upvotes: 1

Fahmi
Fahmi

Reputation: 37473

use union

select name from tableA
union 
select name from tableB

Upvotes: 3

Related Questions