php - 关联两个表

标签 php mysql cakephp cakephp-2.0

我正在使用 CakePHP 2.4。

我在数据库中有两个表,table1.num 和 **table2.num,我需要通过连接查询将它们关联起来。

table1.num 有前缀 DA,比如 DA-12929table2.num 有前缀 DB,如 DB-12929

在我的应用中,我有一个连接

option = array(
                'recursive'  => -1,
                'fields'     => array('Table1.*', 'Table2.*'),
                'joins'      => array(
                    array('table' => 'table2',
                       'alias'       => 'Table2',
                       'type'        => 'INNER',
                       'conditions'  => array(
                            'Tabe1.num = Table2.num',
                        ),
                    ),
                ),
            );

我如何在没有前缀的情况下将它们联系起来?

最佳答案

嗨,我找到了一个解决方案,我只是添加了 SUBSTRING

option = array(
                'recursive'  => -1,
                'fields'     => array('Table1.*', 'Table2.*'),
                'joins'      => array(
                    array('table' => 'table2',
                       'alias'       => 'Table2',
                       'type'        => 'INNER',
                       'conditions'  => array(
                            'SUBSTRING(Tabe1.num,4) = SUBSTRING(Table2.num,4)',
                        ),
                    ),
                ),
            );

关于php - 关联两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45741343/

相关文章:

php - Zend 在 View 助手中重定向

php - 社交网络 - 用户个人资料设计架构问题

MySQL 和 Entity Framework 问题

java - Tomcat JDBC Conencton Pool + MySQL 给出 "Broken pipe"问题,即使有连接验证

cakephp:在哪里放置Configure::write在app_controller中?

php - MongoDB 更新替换文档

security - 在 cakephp 中使用 'low' 安全性有什么含义?

php - 简单的jquery post变量发送

php - 为什么cURL在无效的URL上不返回错误?

mysql - 从文本/Excel/CSV 在 MySQL 表中创建列