Codeigniter select data from multiple tables from database | no join

142
December 08, 2018, at 09:30 AM

So my question is:

Is it possible to select all data from different tables in one query?

Example1:

$query = $this->db->get('table1');
$query = $this->db->get('table2');
$query = $this->db->get('table3');
return $query->result();

Example2:

$this->db->select('*');
$this->db->from('table1', 'table2', 'table3');
$query = $this->db->get();
return $query->result();

I think the second exaple is possible. If not i want to ask how you would do that.

Thank you.

Answer 1

It can be done by putting the table names in an arrary

$query = $this->db
    ->select('*')
    ->from(['table1', 'table2'])
    ->get();
return $query->result();

But the number of rows in the result will be the product of the number of rows in each table, i.e. table1 has 3 rows and table2 has 19 you'll get 57 rows in the result. Are you sure that's what you want?

Joins are easy to write and highly efficient. Don't be afraid of them.

READ ALSO
How to Replicate CountA function in mysql [duplicate]

How to Replicate CountA function in mysql [duplicate]

This question already has an answer here:

156
Complicated Nested SQL query [on hold]

Complicated Nested SQL query [on hold]

I want to write a SQL Query to get Distinct "LEADER NAMES" whose attendance is greater than 50% from two tables: Student (Rno,Name,Leader), Attend (Rno, Attendance)

142
MySQL Update Table WHERE column in another is

MySQL Update Table WHERE column in another is

I have two tables in MySQL and I would like to update a column in one of them to match values from another

131
Fetch data from two diff databases in CI format with cross conditions

Fetch data from two diff databases in CI format with cross conditions

Trying to fetch data in CI format using join on two tables from different DBs, getting error on the same

146