可以使用" LIMIT 1"进行MySQL SELECT查询可能返回一个集而不是单个值?

时间:2014-11-17 19:56:39

标签: mysql sql database cakephp

我正在使用CakePHP的Containable行为并且遇到了一个奇怪的问题。我有这个:

'contain' => array(
            'ExhibitorTypeRequestType' => array(
                'order' => '(SELECT et.type FROM exhibitor_types AS et WHERE ExhibitorTypeRequestType.exhibitor_type_id = et.id LIMIT 1)',
                'ExhibitorType'
            )
        )

抛出SQL错误:

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'SELECT et.type FROM exhibitor_types AS et WHERE ExhibitorTypeRequestType.exhibitor_type_id = et.id LIMIT 1' in 'field list'

有没有人知道为什么MySQL会将此查询视为一个列?

更新

我在项目的其他地方有这个非常相似的查询,也使用Containable,它完美运行:

'contain' => array(
            'Field',
            'User' => array(
                'UserField' => array(
                    'order' => '(SELECT f.no_order FROM fields AS f WHERE UserField.field_id = f.id LIMIT 1)'
                )
            )
        )

1 个答案:

答案 0 :(得分:0)

刚想通了,如果我改变了

'contain' => array(
        'ExhibitorTypeRequestType' => array(
            'order' => '(SELECT et.type FROM exhibitor_types AS et WHERE ExhibitorTypeRequestType.exhibitor_type_id = et.id LIMIT 1)',
            'ExhibitorType'
        )
    )

为:

'contain' => array(
            'ExhibitorTypeRequestType' => array(
                'order' => '(SELECT et.type FROM exhibitor_types AS et WHERE ExhibitorTypeRequestType.exhibitor_type_id = et.id LIMIT 1)',
                'ExhibitorType' => array(
                    'order' => 'type'
                )
            )
        )
奇迹般地工作! 'order' => 'type'在ExhibitorType上下文中绝对不相关,它在某种程度上是必需的。否则,Cake会生成奇怪的SQL错误。