JavaCake
JavaCake

Reputation: 4115

Data from two tables and joining in codeigniter

I have two tables with following costruction:

errortype:

| id | errortype_text |

errorreason:

| id | errorreason_text | errortype_id |

I would like to select all data from errorreason table and replace the errortype_id data with the coresponding errortype_text.

How is this possible?

Upvotes: 0

Views: 1640

Answers (2)

1' OR 1 --
1' OR 1 --

Reputation: 1714

If you are talking about SQL/MySQL it should be:

SELECT `errortype_text`, `errorreason_text`, `errortype_id` FROM `errorreason` JOIN `errortype` ON `errortype`.`id`=`errorreason`.`id`

This joins your tables based on entries having the same id and it is exactly the method Yan suggested.

If you are explicitely referring to PHP/Codeigniter, you have to pass this as a parameter to mysql_query to be afterwards evaluated:

$query=mysql_query("SELECT `errortype_text`, `errorreason_text`, `errortype_id` FROM `errorreason` JOIN `errortype` ON `errortype`.`id`=`errorreason`.`id`");

Upvotes: 1

Christian Giupponi
Christian Giupponi

Reputation: 7618

if the tables have a foreign key you can do that in your model:

$this->db->select('erroreason_text');
$this->db->join('errorreason', 'errortype.id = errorreason.id');
$query = $this->db->get('errortype');
return $query->result();

Upvotes: 3

Related Questions