[英]Data from two tables and joining in codeigniter
I have two tables with following costruction: 我有两个表,下面的构造:
errortype: ERRORTYPE:
| id | errortype_text |
errorreason: 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
. 我想选择的所有数据
errorreason
表并更换errortype_id
与coresponding数据errortype_text
。
How is this possible? 这怎么可能?
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();
If you are talking about SQL/MySQL it should be: 如果您正在谈论SQL / MySQL,则应为:
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. 这将基于具有相同ID的条目联接表,而这正是Yan建议的方法。
If you are explicitely referring to PHP/Codeigniter, you have to pass this as a parameter to mysql_query to be afterwards evaluated: 如果您明确地引用PHP / Codeigniter,则必须将此作为参数传递给mysql_query以便随后进行评估:
$query=mysql_query("SELECT `errortype_text`, `errorreason_text`, `errortype_id` FROM `errorreason` JOIN `errortype` ON `errortype`.`id`=`errorreason`.`id`");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.