php JOIN 语句中的 CodeIgniter ActiveRecord 字段名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10046249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter ActiveRecord field names in JOIN statement
提问by deed02392
I am building a query involving a JOIN. This is the first time I've done db stuff with Active Record and I've hit a bit of a snag.
我正在构建一个涉及 JOIN 的查询。这是我第一次用 Active Record 做 db 的事情,但我遇到了一些障碍。
I want to join a table called companiesto the userstable so I can get the name of the company etc the user is in. I've done this sort of successfully like so:
我想加入一个名为表companies的users表,所以我可以得到公司的名称等,用户在我已经成功地做到了这一点有点像这样:
function get_profile_by_username($username)
{
$this->db->join('companies', $this->table_name.'.company_id = companies.id');
$this->db->where('LOWER(username)=', strtolower($username));
$query = $this->db->get($this->table_name);
if ($query->num_rows() == 1) return $query->row();
return NULL;
}
However the issue being that the fields in companies, they are idand nameare returned in that object as simply called name.
然而问题是,在田野companies,他们id和name该对象被返回简称name。
Normally when I would write the raw query I would give aliases to the tables and the result would be something like u.company_id, c.name. So I'd know namehad nothing to do with the user but of course is the name of the company. And although not an issue now but potentially in the future, the idcolumn obviously can't coexist in the result set, so one gets overwritten!
通常,当我编写原始查询时,我会给表提供别名,结果将类似于u.company_id, c.name. 所以我知道name与用户无关,但当然是公司的名称。虽然现在不是问题,但可能在未来,该id列显然不能在结果集中共存,所以会被覆盖!
How can we get this sort of differentiating between the fields that come from certain tables? Or is there a better way of going about table joins and working with joined query data sets/objects?
我们如何才能区分来自某些表的字段?或者是否有更好的方法来处理表连接和使用连接的查询数据集/对象?
Edit:
编辑:
If I was doing it as a raw query I'd do:
如果我将其作为原始查询进行,我会这样做:
SELECT u.id, u.username, c.name
FROM users AS u
JOIN companies AS c
ON c.id = u.company_id
WHERE u.username = 'foobar';
Which is great but if I tried to do that in active record I reckon that's pretty poor practice, if it works at all.
这很好,但如果我试图在活动记录中这样做,我认为这是非常糟糕的做法,如果它真的有效的话。
回答by safarov
If you want to select some specific columns from table use db->select(). You can give alias to tables, add some conditions and etc. Send second parameter FALSEto not escape special characters.
如果要从表中选择一些特定的列,请使用db->select(). 您可以为表提供别名,添加一些条件等。发送第二个参数FALSE以不转义特殊字符。
$this->db->select('u.id, u.username, c.name', false);
$this->db->from('user as u');
$this->db->join('companies as c', 'u.company_id = c.id');
$this->db->where('LOWER(u.username)=', strtolower('foobar'));
$query = $this->db->get();
回答by Jahaziel Campos
$this->db->select('ut.nombre as nombreu, ut.apellido, ru.nombre as nombrer');
$this->db->from('User_table ut');
$this->db->join('Role_usuario ru', 'ut.role_user = ru.Id');
$query = $this->db->get();`
`
`

