开始

Where 语法

查询

事务

高级

PDO 对象

Debug

数据库信息

版本: 1.1.3

select

从数据库中选择数据

select($table, $columns, $where)
select($table, $join, $columns, $where)
返回: [array]
你可以使用 "*" 作为 columns 参数以获取全部字段的数据,但最好能提供目标字段列表以提高查询性能。
$datas = $database->select("account", [
	"user_name",
	"email"
], [
	"user_id[>]" => 100
]);

// $datas = array(
// 	[0] => array(
// 		"user_name" => "foo",
// 		"email" => "[email protected]"
// 	),
// 	[1] => array(
// 		"user_name" => "cat",
// 		"email" => "[email protected]"
// 	)
// )

foreach($datas as $data)
{
	echo "user_name:" . $data["user_name"] . " - email:" . $data["email"] . "<br/>";
}

// 选择所有字段
$datas = $database->select("account", "*");

// 选择一个字段
$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"],

	// 如果你要连接一个表以别的值,你必须指定一个别名给表
	"[>]account (replyer)" => ["replyer_id" => "user_id"],

	// 你可以通过在字段前加上表名称来指向前面已连接的表
	"[>]account" => ["author_id" => "user_id"],
	"[>]album" => ["account.user_id" => "user_id"],

	// 多个条件
	"[>]account" => [
		"author_id" => "user_id",
		"album.user_id" => "user_id"
	]
], [
	"post.post_id",
	"post.title",
	"account.user_id",
	"account.city",
	"replyer.user_id",
	"replyer.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

数据映射

自定义输出的数据结构 - 包装数据的键名与字段本身没有关系,它是多维的。

$data = $database->select("post", [
	"[>]account" => ["user_id"]
], [
	"post.post_id",
	"post.content",

	"userData" => [
		"account.user_id",
		"account.email",

		"meta" => [
			"account.location",
			"account.gender"
		]
	]
], [
	"LIMIT" => [0, 2]
]);

echo json_encode($data);

// 输出的数据
[
	{
		post_id: "1",
		content: "Hello world!",
		userData: {
			user_id: "1",
			email: "[email protected]",
			meta: {
				location: "New York",
				gender: "male"
			}
		}
	},
	{
		post_id: "2",
		content: "Hey everyone",
		userData: {
			user_id: "2",
			email: "[email protected]",
			meta: {
				location: "London",
				gender: "female"
			}
		}
	}
]

别名

您可以使用别名来替换旧的字段或表名称。 这在表连接中防止名称冲突很有用。

$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 (content)", [
	"[>]account (user)" => "user_id",
], [
	"content.user_id (author_id)",
	"user.user_id"
], [
	"LIMIT" => 20
]);

// SELECT
// 	"content"."user_id" AS author_id,
// 	"user"."user_id"
// FROM
// 	"post" AS "content"
// LEFT JOIN "account" AS "user" USING ("user_id")
// LIMIT 2

// $data = array(
// 	[0] => array(
// 		"author_id" => "1",
// 		"user_id" => "321"
// 	),
// 	[1] => array(
// 		"author_id" => "2",
// 		"user_id" => "322"
// 	)
// )