sql 基礎查詢
一旦有了連線例項就可以通過[[yii\db\command]]執行 sql 查詢。
select 查詢
查詢返回多行:
$command = $connection->createcommand('select * from post');
$posts = $command->queryall();
返回單行:
$command = $connection->createcommand('select * from post where id=1');
$post = $command->queryone();
查詢多行單值:
$command = $connection->createcommand('select title from post');
$titles = $command->querycolumn();
查詢標量值/計算值:
$command = $connection->createcommand('select count(*) from post');
$postcount = $command->queryscalar();
update, insert, delete 更新、插入和刪除等
如果執行 sql 不返回任何資料可使用命令中的 execute 方法:
$command = $connection->createcommand('update post set status=1 where id=1');
$command->execute();
你可以使用insert,update,delete 方法,這些方法會根據引數生成合適的sql並執行.
// insert
$connection->createcommand()->insert('user', [
'name' => 'sam',
'age' => 30,
])->execute();
// insert 一次插入多行
$connection->createcommand()->batchinsert('user', ['name', 'age'], [
['tom', 30],
['jane', 20],
['linda', 25],
])->execute();
// update
$connection->createcommand()->update('user', ['status' => 1], 'age > 30')->execute();
// delete
$connection->createcommand()->delete('user', 'status = 0')->execute();
Yii2 查詢條件
字串格式,例如 status 1 雜湊格式,例如 status 1,type 2 操作符格式,例如 like name test andfilterwhere between updated at oldtime,current andwhere between updated at oldtime...
sql基礎查詢
1.查詢 northwind 資料庫employees 表中名以 a開頭的雇員的姓名。use northwind goselect firstname,lastname from employees where firstname like a go 2.使用演示指令碼建立表 插入資料,查詢以 x ...
第2章 查詢基礎 SQL基礎教程
select 列名 from 表名 select product id,product name,purchase price from product 查詢多列時,需要使用逗號進行分隔。查詢結果中列的順序和select 子句中的順序相同查詢全部的列 select from 表名 select fr...