0

如何在 Zend\Db\Sql 中创建复杂的 WHERE 关闭?那些带有“COLLATE utf_general_ci”或“REGEXP”的人。还有什么方法可以将查询作为我想要的字符串提供给它?

谢谢!

4

1 回答 1

2

完整复杂 where 子句的示例会有所帮助,但您可以使用 Expression 或 Literal

启动适配器

$adapter = new Zend\Db\Adapter\Adapter(array(
    'driver' => 'Mysqli',
    'database' => 'zend_db_example',
    'username' => 'developer',
    'password' => 'developer-password'
 ));

$sql = new Zend\Db\Sql\Sql($adapter);

$qi = function($name) use ($adapter) { return $adapter->platform->quoteIdentifier( $name ); };

$qv = function($name) use ($adapter) { return $adapter->platform->quoteValue( $name ); };

文字

$regexSpecification = '(\\"key\\":\\"value\\")';
$sql->select()
        ->from('dbTable')
        ->where->literal( $qi('column1') . " REGEXP " . $qv($regexSpecification) )
        ->or->equalTo('column2', 'value');

//SELECT `dbTable`.* FROM `dbTable` WHERE `column1` REGEXP '(\"key\":\"value\")' OR `column2` = 'value'

表达

    $expression = new Zend\Db\Sql\Predicate\Expression( "SHA2(".$qi('column2').", 512)" );
    $sql->select()
        ->from('dbTable')
        ->columns( array( 'hashed' => $expression ) )
        ->where( array( $qi('column1') . " COLLATE latin1_general_cs = " . $qv('value'))); 

//SELECT SHA2(`column2`, 512) AS `hashed` FROM `dbTable` WHERE `column1` COLLATE latin1_general_cs = 'value'

适配器执行的查询

$adapter->query( 'ALTER TABLE ADD INDEX('. $qi('foo') .') ON ( '. $qi('foo_column') .' )', Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE );

参考:

https://packages.zendframework.com/docs/latest/manual/en/modules/zend.db.adapter.html

https://packages.zendframework.com/docs/latest/manual/en/modules/zend.db.sql.html

于 2013-06-18T22:27:40.040 回答