yii\db\Query::from PHP Method

from() public method

Sets the FROM part of the query.
public from ( string | array $tables )
$tables string | array the table(s) to be selected from. This can be either a string (e.g. `'user'`) or an array (e.g. `['user', 'profile']`) specifying one or several table names. Table names can contain schema prefixes (e.g. `'public.user'`) and/or table aliases (e.g. `'user u'`). The method will automatically quote the table names unless it contains some parenthesis (which means the table is given as a sub-query or DB expression). When the tables are specified as an array, you may also use the array keys as the table aliases (if a table does not need alias, do not use a string key). Use a Query object to represent a sub-query. In this case, the corresponding array key will be used as the alias for the sub-query. Here are some examples: ```php // SELECT * FROM `user` `u`, `profile`; $query = (new \yii\db\Query)->from(['u' => 'user', 'profile']); // SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`; $subquery = (new \yii\db\Query)->from('user')->where(['active' => true]) $query = (new \yii\db\Query)->from(['activeusers' => $subquery]); // subquery can also be a string with plain SQL wrapped in parenthesis // SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`; $subquery = "(SELECT * FROM `user` WHERE `active` = 1)"; $query = (new \yii\db\Query)->from(['activeusers' => $subquery]); ```
    public function from($tables)
    {
        if (!is_array($tables)) {
            $tables = preg_split('/\\s*,\\s*/', trim($tables), -1, PREG_SPLIT_NO_EMPTY);
        }
        $this->from = $tables;
        return $this;
    }

Usage Example

 public function actionIndex()
 {
     $query = new Query();
     /*$department = $query->select(['department_id','department_name'])
       ->from('department')
       ->all();*/
     //$department = $query->from('department')->all(); //เทียบได้กับโดยไม่ต้องใส่ select ใส่ from เลย
     //yii จะใส่ select ให้ SELECT * FROM department
     /*$search ='กลุ่ม'
       $department = $query->select(['department_id','department_name'])
                           ->from('department')
                           ->where([
                               '>like','department_id',$search
                               ])
                           ->all();*/
     //order by อีกหนึ่งวิธี
     //$department = $query->from('department_id')->orderBy('department_id DESC')->all();
     $department = $query->from('department')->orderBy(['department_name' => SORT_ASC])->all();
     $count = $query->from('department')->count();
     $max = $query->from('department')->max('department_id');
     $sum = $query->from('department')->sum('department_id');
     // echo sql
     $sql = $query->from('department')->orderBy(['department_name' => SORT_ASC])->createCommand();
     return $this->render('index', ['departments' => $department, 'count' => $count, 'max' => $max, 'sum' => $sum, 'sql' => $sql]);
 }
All Usage Examples Of yii\db\Query::from