http://php.hyphp.cn/237479
<?php
namespace Action;
use HY\Action;
class IndexAction extends Action {
public function Index(){
//实例User表为对象
$User = M("User");
// [>] == LEFT JOIN
// [<] == RIGH JOIN
// [<>] == FULL JOIN
// [><] == INNER JOIN
$User->select("post", array(
// Here is the table relativity argument that tells the relativity between the table you want to join.
// The row author_id from table post is equal the row user_id from table account
"[>]account" => array("author_id" => "user_id"),
// The row user_id from table post is equal the row user_id from table album.
// This is a shortcut to declare the relativity if the row name are the same in both table.
"[>]album" => "user_id",
// [post.user_id is equal photo.user_id and post.avatar_id is equal photo.avatar_id]
// Like above, there are two row or more are the same in both table.
"[>]photo" => array("user_id", "avatar_id"),
// If you want to join the same table with different value,
// you have to assign the table with alias.
"[>]account (replyer)" => array("replyer_id" => "user_id"),
// You can refer the previous joined table by adding the table name before the column.
"[>]account" => array("author_id" => "user_id"),
"[>]album" => array("account.user_id" => "user_id"),
// Multiple condition
"[>]account" => array(
"author_id" => "user_id",
"album.user_id" => "user_id"
)
), array(
"post.post_id",
"post.title",
"account.user_id",
"account.city",
"replyer.user_id",
"replyer.city"
), array(
"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
}
}
自己可以用DEBUG 尝试输出语句 是否正常
|