我需要连接两个表,其中基表的连接列值的子字符串用于与另一个表连接。
我使用的是 zend Framework 2,我编写的查询如下。
$select = new Select($this->table);
$select->columns(array('match_code'))
->join('countries','countries.cid = SUBSTR(matches.teams,1,3)', 'name')
->where(array('round'=>1));
这会产生错误。
SQLSTATE[42000]: Syntax error or access violation: 1630 FUNCTION mydatabase.SUBSTR does not exist.
Check the 'Function Name Parsing and Resolution' section in the Reference Manual
我已经运行了与此相对应的 mysql 查询,并且它运行良好。
SELECT `matches`.`match_code`, `countries`.`name`
FROM `matches` INNER JOIN `countries`
ON `countries`.`cid`=SUBSTR(`matches`.`teams`,1,3)
WHERE `round` = 1
我不能在这里使用 Zend\Db\Sql\Expression
,因为 join()
只需要一个字符串。
有什么建议吗?
最佳答案
我通过在 join()
的 ON
子句中使用 Zend\Db\Sql\Expression
解决了这个问题。
$select->columns(array('match_code'))
->join('countries',new \Zend\Db\Sql\Predicate\Expression('countries.cid = SUBSTR(matches.teams,1,3)'), 'name')
->where(array('round'=>1));
感谢@Crisp 的建议。
关于mysql - 在 mysql JOIN 查询的 'ON' 子句中使用 mysql 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23491807/