简体   繁体   English

在join语句中提供别名而不更改codeigniter中的select语句(*)

[英]Give an alias in a join statement without changing the select statement (*) in codeigniter

I'm working with queries in espressionengine, and I'm coding them with the codeigniter syntax. 我正在使用espressionengine中的查询,并使用codeigniter语法对其进行编码。 Here's my query: 这是我的查询:

$sql = ee()->db->select('*');
$sql = ee()->db->from('sometable');
$sql = ee()->db->where('id', $hidden_id);
$sql = ee()->db->get();

I'd like to add a join in order to get another value like that: 我想添加一个联接,以获得另一个像这样的值:

 $sql = ee()->db->join('another_table', 'sometable.another_table_id, another_table.id);

Now the problem is that in the join statement I add another column with the same name (id). 现在的问题是,我在join语句中添加了另一个具有相同名称(id)的列。

Instead of changing the select statement (*) I'd like to add an alias in the join statement something like: 我不想更改select语句(*),而是想在join语句中添加一个别名,例如:

 $sql = ee()->db->join('another_table', 'sometable.another_table_id, another_table.id as another_table_id);

Is it doable? 可以吗

Hope this will help you : 希望这个能对您有所帮助 :

And your query should be like this : 您的查询应如下所示:

Use this if both primary and foreign key has same column name : 如果主键和外键具有相同的列名,请使用此选项:

$this->db->select('*');
$this->db->from('sometable stable');
$this->db->join('(SELECT id  
FROM another_table ) as a_table' ,'id');
$query = $this->db->get();
print_r($query->result());

if not please use this : 如果没有,请使用此:

Make sure in join here a_table.id = stable.id , primary and foreign key match here 确保在此处加入a_table.id = stable.id ,此处的primary键和foreign键匹配

$this->db->select('*,a_table.id as credit_id');
$this->db->from('sometable stable');
$this->db->join('another_table a_table','a_table.id = stable.id');
$query = $this->db->get();


print_r($query->result());

You can also get result from this query with the help of $this->db->query(); 您还可以在$this->db->query();的帮助下从此查询中获得结果$this->db->query();

$sql = 'your query here';
$query = $this->db->query();
print_r($query->result());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM