yii FULLTEXT索引匹配查询

时间:2013-11-19 18:58:08

标签: mysql sql yii match against

我想在yii框架中创建一个如下所示的SQL查询:

mysql> SELECT id, body, MATCH (title,body) AGAINST
-> ('Security implications of running MySQL as root') AS score
-> FROM articles WHERE MATCH (title,body) AGAINST
-> ('Security implications of running MySQL as root');

我试试这个,但它不起作用:

    $dataProvider = new CActiveDataProvider('data', array(
            'criteria'=>array(
                'condition' => 'MATCH (title,body) AGAINST ('Security implications of running MySQL as root') AS score', 
WHERE MATCH (title,body) AGAINST ('Security implications of running MySQL as root')
                'limit' => '20',            ),
            'pagination' => false
        ));

2 个答案:

答案 0 :(得分:2)

http://www.yiiframework.com/doc/api/1.1/CDbCriteria#condition-detail

  

这是指SQL语句中的WHERE子句

所以应该只有MATCH (title,body) AGAINST ('Security implications of running MySQL as root')

http://www.yiiframework.com/doc/api/1.1/CDbCriteria#select-detail

  

这是指SQL语句中的SELECT子句

这是id, body, MATCH (title,body) AGAINST ('Security implications of running MySQL as root') AS score

的地方

答案 1 :(得分:0)

首先,您需要对搜索列执行ALTER查询。

 ALTER TABLE item ADD FULLTEXT(title,description,location);

查询搜索

 $query = \common\models\Item::find()
             ->where(['is_active'=>'1']);
 $query->andWhere("MATCH(title,description,location) AGAINST 
 ('$searchKeywords' IN BOOLEAN MODE)");