【问题标题】:CodeIgniter Active Record; Merge two queries into one?CodeIgniter 活动记录;将两个查询合并为一个?
【发布时间】:2016-08-11 10:09:16
【问题描述】:

在我的第一个查询中,我遍历所有具有statusopenchannel_id23 的记录,得到field_id_299 列。字段field_id_299 是另一个channelentry_id

第二个查询,然后使用这个field_id_299 AS entry_id,得到field_id_36列。

是否可以在一个查询中完成所有这些操作?

$query = ee()->db->select('cd.field_id_299')
             ->from('exp_channel_titles AS ct')
             ->join('exp_channel_data AS cd', 'ct.entry_id = cd.entry_id', 'left')
             ->where('ct.status', 'open')
             ->where('ct.channel_id', '23')                  
             ->get();

$query = ee()->db->select('field_id_36')
             ->from('exp_channel_data')
             ->where('entry_id', $entry_id)
             ->get();

【问题讨论】:

  • 我不确定.. 但你能在 mysql 查询下运行并确认这是否是你想要的结果。 select field_id_36 from exp_channel_data where entry_id in ( select cd.field_id_299 from exp_channel_titles ct left join exp_channel_data cd on ct.entry_id = cd.entry_id where ct.status='open' and channel_id = 23);

标签: mysql sql codeigniter codeigniter-2


【解决方案1】:

下面试试

$query = ee()->db->select('cd1.field_id_36')
             ->from('exp_channel_titles AS ct')
             ->join('exp_channel_data AS cd', 'ct.entry_id = cd.entry_id', 'left')
             ->join('exp_channel_data AS cd1', 'cd.field_id_299 = cd1.entry_id', 'left')
             ->where('ct.status', 'open')
             ->where('ct.channel_id', '23')                  
             ->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多