上一篇《WHERE语法-Medoo使用指南》中介绍了Medoo的WHERE语法,本篇将要对Select方法进行说明。Select API主要用于设定提取数据的目标字段。php
选取方法:Select html
从数据库中选取数据。数据库
select($table, $columns, $where) //table [string]: 表名 //columns [string/array]: 将要提取的数据的目标字段 //where (可选) [array]: 过滤记录的WHERE子句 select($table, $join, $columns, $where) join [array]: 表链接相关的表名。若是不须要链接,则忽略它。
返回值: [array]post
提示:您能够使用“*”为列参数,来获取全部的列,但为了提升性能,提供目标列的话要好得多。性能
$database = new medoo("my_database"); $datas = $database->select("account", [ "user_name", "email" ], [ "user_id[>]" => 100 ]); // $datas = array( // [0] => array( // "user_name" => "foo", // "email" => "foo@bar.com" // ), // [1] => array( // "user_name" => "cat", // "email" => "cat@dog.com" // ) // ) foreach($datas as $data) { echo "user_name:" . $data["user_name"] . " - email:" . $data["email"] . "<br/>"; } // 选取全部列 $datas = $database->select("account", "*"); // 选取单列user_name $datas = $database->select("account", "user_name"); // $datas = array( // [0] => "foo", // [1] => "cat" // ) // [表链接] // SQL-JOIN子句可以把两个表之间的行绑定在一块儿。Medoo为JOIN子句提供了简单的语法。 // [>] == LEFT JOIN // [<] == RIGH JOIN // [<>] == FULL JOIN // [><] == INNER JOIN $database->select("post", [ // 这是相关联的表的参数,它代表了你想要进行链接的表之间的关联性。 // post.author_id 等于 account.user_id "[>]account" => ["author_id" => "user_id"], // post.user_id 等于 album.user_id // 若是两个表中的字段名是相同的,那么你能够使用如下这种快捷的方式来声明关联性。 "[>]album" => "user_id", // [post.user_id 等于 photo.user_id, 而且 post.avatar_id 等于 photo.avatar_id] // 和上面同样,在各个表中的字段名都是相同的 "[>]photo" => ["user_id", "avatar_id"] ], [ "post.post_id", "post.title", "account.city" ], [ "post.user_id" => 100, "ORDER" => "post.post_id DESC", "LIMIT" => 50 ]); // SELECT // `post`.`post_id`, // `post`.`title`, // `account`.`city` // FROM `post` // LEFT JOIN `account` ON `post`.`author_id` = `account`.`user_id` // LEFT JOIN `album` USING (`user_id`) // LEFT JOIN `photo` USING (`user_id`, `avatar_id`) // WHERE // `post`.`user_id` = 100 // ORDER BY `post`.`post_id` DESC // LIMIT 50 //[列的别名 - 自Medoo 0.9.1起支持] // 你能够使用列别名做为一个新的列名,用于替代原来的。 // 这在表链接的时候,用来防止列名冲突很是有用。 $data = $database->select("account", [ "user_id", "nickname(my_nickname)" ], [ "LIMIT" => 20 ]); // $data = array( // [0] => array( // "user_id" => "1", // "my_nickname" => "foo" // ), // [1] => array( // "user_id" => "2", // "my_nickname" => "bar" // ) // ) $data = $database->select("post", [ "[>]account" => "user_id", ], [ "post.user_id(post_user_id)", "account.user_id(account_user_id)" ], [ "LIMIT" => 20 ]); // $data = array( // [0] => array( // "post_user_id" => "1", // "account_user_id" => "321" // ), // [1] => array( // "post_user_id" => "2", // "account_user_id" => "322" // ) // )
原文标题:选取方法:Select API-Medoo使用指南spa
原文连接:http://loiy.net/post/572.html.net