在 codeigniter 查询生成器中使用联合,并在虚拟 mysql 列中进行过滤
Using union in codeigniter query builder, and filter in a virtual mysql column
在我的项目中,我使用带有服务器端处理的数据tables 插件。它工作正常,直到我进行搜索或排序(排序)操作,因为它需要活动记录才能做到这一点。
我的场景是,我有一个账户table,收入table和付款table,我想查看收入和付款的所有数据table ,这就是为什么我需要一个工会。我的查询如下---
SELECT 'Income' as source, fld_type, fld_amount, ta.fld_account as account, fld_date, tbl_revenue.fld_description as fld_traninfo, tbl_revenue.fld_created as created
FROM tbl_revenue JOIN tbl_accounts as ta on tbl_revenue.fld_account_id = ta.fld_id
UNION
SELECT 'Expense' as source, fld_type, fld_amount, tae.fld_account, fld_date, tbl_payment.fld_description as fld_traninfo, tbl_payment.fld_created as created
FROM tbl_payment JOIN tbl_accounts as tae on tbl_payment.fld_account_id = tae.fld_id
有什么方法可以在此查询中使用查询生成器吗?
第二个问题,您可以看到我创建了一个名为 'source' 的虚拟列,我想使用 where 子句过滤此列并附加此查询,如下所示
WHERE source like "%a%" limit(10,0)
但是这个returns我没有任何列名'source',我如何过滤这个列?
感谢任何帮助。
有一种方法可以做到这一点,但它有点 hacky 因为 codeigniter 的 querybuilder 会在查询中添加一个自动 SELECT
语句你自己
为了获得您想要的结果,您必须将 select 语句拆分为 2 个查询并将 where 子句添加到此查询
类似的东西应该可以工作:
$strQuery1 = $this->db
->select('income as source, fld_type, fld_amount, ta.fld_account as account, fld_date, tbl_revenue.fld_description as fld_traninfo, tbl_revenue.fld_created as created')
->from('tbl_revenue')
->join('tbl_accounts as ta', 'tbl_revenue.fld_account_id = ta.fld_id')
->get_compiled_select();
$strQuery2 = $this->db
->select('Expense as source, fld_type, fld_amount, ta.fld_account as account, fld_date, tbl_revenue.fld_description as fld_traninfo, tbl_revenue.fld_created as created')
->from('tbl_payment')
->join('tbl_accounts as ta', 'tbl_revenue.fld_account_id = ta.fld_id')
->get_compiled_select();
$strWhere = substr($this->db->like('source', 'a', 'both')->get_compiled_select(), 8);
$query = $this->db->query($strQuery1.' UNION '.$strQuery2.$strWhere);
在我的项目中,我使用带有服务器端处理的数据tables 插件。它工作正常,直到我进行搜索或排序(排序)操作,因为它需要活动记录才能做到这一点。
我的场景是,我有一个账户table,收入table和付款table,我想查看收入和付款的所有数据table ,这就是为什么我需要一个工会。我的查询如下---
SELECT 'Income' as source, fld_type, fld_amount, ta.fld_account as account, fld_date, tbl_revenue.fld_description as fld_traninfo, tbl_revenue.fld_created as created
FROM tbl_revenue JOIN tbl_accounts as ta on tbl_revenue.fld_account_id = ta.fld_id
UNION
SELECT 'Expense' as source, fld_type, fld_amount, tae.fld_account, fld_date, tbl_payment.fld_description as fld_traninfo, tbl_payment.fld_created as created
FROM tbl_payment JOIN tbl_accounts as tae on tbl_payment.fld_account_id = tae.fld_id
有什么方法可以在此查询中使用查询生成器吗?
第二个问题,您可以看到我创建了一个名为 'source' 的虚拟列,我想使用 where 子句过滤此列并附加此查询,如下所示
WHERE source like "%a%" limit(10,0)
但是这个returns我没有任何列名'source',我如何过滤这个列?
感谢任何帮助。
有一种方法可以做到这一点,但它有点 hacky 因为 codeigniter 的 querybuilder 会在查询中添加一个自动 SELECT
语句你自己
为了获得您想要的结果,您必须将 select 语句拆分为 2 个查询并将 where 子句添加到此查询
类似的东西应该可以工作:
$strQuery1 = $this->db
->select('income as source, fld_type, fld_amount, ta.fld_account as account, fld_date, tbl_revenue.fld_description as fld_traninfo, tbl_revenue.fld_created as created')
->from('tbl_revenue')
->join('tbl_accounts as ta', 'tbl_revenue.fld_account_id = ta.fld_id')
->get_compiled_select();
$strQuery2 = $this->db
->select('Expense as source, fld_type, fld_amount, ta.fld_account as account, fld_date, tbl_revenue.fld_description as fld_traninfo, tbl_revenue.fld_created as created')
->from('tbl_payment')
->join('tbl_accounts as ta', 'tbl_revenue.fld_account_id = ta.fld_id')
->get_compiled_select();
$strWhere = substr($this->db->like('source', 'a', 'both')->get_compiled_select(), 8);
$query = $this->db->query($strQuery1.' UNION '.$strQuery2.$strWhere);