簡體   English   中英

Yii2 SELECT * WHERE id = array()?

[英]Yii2 SELECT * WHERE id = array()?

我有查詢 select 使用 WHERE = array(1,2,...),我試過 IN 但只有結束 ID 的數據和前一個 ID 的數據沒有出現。 幫幫我,謝謝大家。

我的代碼:

Controller:

public function actionGetServiceType($q = null, $id = null) {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!is_null($q)) {
            $queryTest = new Query;
            $queryTest->select('service_type_id')
                ->from('link_service_group_all')
                ->where(['IN', 'service_group_id', $id]);
            $query = new Query;
            $query->select('id as id, title AS text')
                ->from('service_type')
                ->where(['like', 'title', $q])
                ->andWhere(['IN', 'id' , $queryTest]);
            $command = $query->createCommand();
            $data = $command->queryAll();
            $out['results'] = array_values($data);
        }
        return $out;
    }
public function actionGetServiceType($q = null, $id = null) {
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    $out = ['results' => ['id' => '', 'text' => '']];
    if (!is_null($q)) {
        $queryTest = new Query;
        $queryTest->select('service_type_id')
            ->from('link_service_group_all')
            ->where(['IN', 'service_group_id', $id]);
        
        $queryTest = array_values($queryTest->createCommand()->queryColumn());
        
        $query = new Query;
        $query->select('id as id, title AS text')
            ->from('service_type')
            ->where(['like', 'title', $q])
            ->andWhere(['IN', 'id' , $queryTest]);
        $command = $query->createCommand();
        $data = $command->queryAll();
        $out['results'] = array_values($data);
    }
    return $out;
}

你錯過了這樣的一行

$queryTest = array_values($queryTest->createCommand()->queryColumn());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM