[英]How to convert named inner join query to code-igniter syntax?
我有以下查询
$query = $this->db->query(
'SELECT ii.json
FROM inventory ii
INNER JOIN
(
SELECT json, MAX(id) as MAX_ID
FROM inventory
GROUP BY business_unit
) group_json ON ii.id = group_json.MAX_ID
INNER JOIN business_units
ON ii.business_unit = business_units.id'
);
return $query->result_array();
我们不得不迁移到MSSQL测试服务器,不幸的是语法不正确。 为了避免将来发生这种情况,我想将其转换为CodeIgniter类型的查询。
任何人都可以告诉我该查询在CodeIgniter中的外观如何,我已经尝试了一些方法,但是没有任何进展。
SELECT ii.json
FROM inventory ii
INNER JOIN
(
SELECT json, MAX(id) as MAX_ID
FROM inventory
GROUP BY business_unit, json --<-- "json" needs to be in GROUP BY
) group_json -- Since it is in SELECT but not
ON ii.id = group_json.MAX_ID -- contained in any aggregate function
INNER JOIN business_units
ON ii.business_unit = business_units.id
如果在SELECT
使用任何聚合函数(MAX,MIN,AVG,SUM,COUNT),则该选择中任何聚合函数中未包含的任何其他列,都必须进入查询的GROUP BY
子句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.