问题:
我真的想坚持使用ActiveRecord而不是直接使用SQL。。有人能帮我把这个转换成activerecord吗?
正在尝试从另一个表中获取电子邮件地址和联系人姓名。map_userfields表是一对多的,每个p.id有多行。每p.id一行。
请参阅此屏幕截图以获取对“映射用户字段”表的引用:
当前非活动记录查询

SELECT
p.id,
(SELECT uf.fieldvalue FROM map_userfields uf WHERE uf.pointid = p.id AND uf.fieldid = 20) As ContactName,
(SELECT uf.fieldvalue FROM map_userfields uf WHERE uf.pointid = p.id AND uf.fieldid = 31) As ContactEmail
FROM
map_points p
WHERE
/** $pointCategory is an array of categories to look for **/
p.type IN($pointCategory)

注意:我使用的是CodeIgniter 2.1.x、MySQL 5.x、php 5.3

最佳答案

只要告诉CI不要解析字符串,就可以选择CI的activeRecords所需的任何内容

$this->db->select('p.id,
(SELECT uf.fieldvalue FROM map_userfields uf WHERE uf.pointid = p.id AND uf.fieldid = 20) As ContactName,
(SELECT uf.fieldvalue FROM map_userfields uf WHERE uf.pointid = p.id AND uf.fieldid = 31) As ContactEmail
',TRUE);
$this->db->from('map_points p');
$this->db->where_in('p.type',$pointCategory);
$q = $this->db->get();

关于mysql - CodeIgniter事件记录查询WITH/子查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9998163/

10-09 22:01