搜索 |
数据库查询
插入数据到表中
可以同时插入多条数据.
- $last_user_id = $database->insert("account", [
- [
- "user_name" => "foo",
- "email" => "foo@bar.com",
- "age" => 25,
- "city" => "New York",
- "(JSON) lang" => ["en", "fr", "jp", "cn"]
- ],
- [
- "user_name" => "bar",
- "email" => "bar@foo.com",
- "age" => 14,
- "city" => "Hong Kong",
- "(JSON) lang" => ["en", "jp", "cn"]
- ]
- ]);
修改表数据
- $database->update("account", [
- "type" => "user",
- // All age plus one
- "age[+]" => 1,
- // All level subtract 5
- "level[-]" => 5,
- // All score multiplied by 2
- "score[*]" => 2,
- // Like insert, you can assign the serialization
- "lang" => ["en", "fr", "jp", "cn", "de"],
- "(JSON) fav_lang" => ["en", "fr", "jp", "cn", "de"],
- // You can also assign # for using SQL functions
- "#uid" => "UUID()"
- ], [
- "user_id[<]" => 1000
- ]);
删除表中的数据
- $database->delete("account", [
- "AND" => [
- "type" => "business"
- "age[<]" => 18
- ]
- ]);
将新的数据替换旧的数据
- $database->replace("account", "type", "user", "new_user", [
- "user_id[>]" => 1000
- ]);
- $database->replace("account", "type", [
- "user" => "new_user",
- "business" => "new_business"
- ], [
- "user_id[>]" => 1000
- ]);
- $database->replace("account", [
- "type" => [
- "user" => "new_user",
- "business" => "new_business"
- ],
- "group" => [
- "groupA" => "groupB"
- ]
- ], [
- "user_id[>]" => 1000
- ]);
从表中返回一行数据
- $email = $database->get("account", "email", [
- "user_id" => 1234
- ]);
- // $email = "foo@bar.com"
- $profile = $database->get("account", [
- "email",
- "gender",
- "location"
- ], [
- "user_id" => 1234
- ]);
确定数据是否存在
- if ($database->has("account", [
- "AND" => [
- "OR" => [
- "user_name" => "foo",
- "email" => "foo"
- ],
- "password" => "12345"
- ]
- ]))
- {
- echo "Password is correct.";
- }
- else
- {
- echo "Password error.";
- }
获取数据表中的行数
- $count = $database->count("account", [
- "gender" => "female"
- ]);
获得数据表中,值最大的
- $max = $database->max("account", "age", [
- "gender" => "female"
- ]);
获得某个列中的最小的值
获得某个列字段的平均值
某个列字段相加
$total = $database->sum("account", "money");
执行sql语句
$data = $database->query("SELECT email FROM account")->fetchAll();//查询数据
输入sql语句,但不执行
Return: 开启Medoo调试模式
输出sql语句,不需要使用echo或其它方法。调试完成请移除此代码
获得最后一个执行的错误.
返回所有执行的查询。
Return: 开启Medoo调试模式
可以用此函数来查询、调试SQL语句
返回最后一条执行的SQL语句.
- $database->select("account", [
- "user_name",
- "email"
- ], [
- "user_id[<]" => 20
- ]);
- $database->insert("account", [
- "user_name" => "foo",
- "email" => "foo@bar.com"
- ]);
- echo $database->last_query();