查询构造器

查询构建器建立在 Star_Model_Abstract 基础之上,可让你创建 程序化的、DBMS无关的SQL语句。相比于原生的SQL语句,查询构建器可以帮你 写出可读性更强的SQL相关的代码,并生成安全性更强的SQL语句,可以非常直观知道具体执行SQL语句,便于调试。

如下所示代码是查询构造器的一个典型用法:

  1. //实例化select
  2. $select = $this->select();
  3. //构造SELECT语句
  4. $select->from($this->getTableName() . " as u")
  5. ->where("age > ?", 18)
  6. ->order("age ASC")
  7. ->limitPage(1, 20);
  8. //查询SQL
  9. $this->fetchAll($select);

上面的代码将会生成并执行如下SQL语句:

  1. SELECT u.* FROM `users` `u` WHERE (age > "18") ORDER BY age ASC LIMIT 20 OFFSET 0
提示: echo $select对象,可以直接打印上面SQL语句,便于SQL调试

查询指定字段

  1. //SELECT u.`username`, u.`age` FROM `users` `u`
  2. $select->from($this->getTableName() . " as u", array("username", "age"));
  3. //SELECT username, age FROM `users`
  4. $select->from($this->getTableName(), "username, age");
  5. //SELECT users.`username` `name`, users.`age` FROM `users`
  6. $select->from($this->getTableName(), array("name" => "username", "age"));

条件查询(where、orWhere)

  1. //SELECT users.* FROM `users` WHERE (age > 18) AND (age < 30)
  2. $select->from($this->getTableName())
  3. ->where("age > 18")
  4. ->where("age < 30");
  5. //SELECT users.* FROM `users` WHERE (username = "john") OR (username = "james")
  6. $select->from($this->getTableName())
  7. ->where("username = ?", "john")
  8. ->orwhere("username = ?", "james");
警告:除非明确参数是安全可靠的,否则绝对禁止$select->where("username = '{$username}'")方式传参,避免您的应用程序受到注入攻击

排序(order)

  1. //SELECT users.* FROM `users` WHERE (age > "18") ORDER BY age ASC
  2. $select->from($this->getTableName())
  3. ->where("age > ?", 18)
  4. ->order("age ASC");
  5. //----------多个排序---------
  6. //SELECT users.* FROM `users` WHERE (age > "18") ORDER BY age ASC, username ASC
  7. $select->from($this->getTableName())
  8. ->where("age > ?", 18)
  9. ->order(array("age ASC", "username ASC"));

group by

  1. //SELECT users.* FROM `users` WHERE (age > 18) GROUP BY age ORDER BY age ASC
  2. $select->from($this->getTableName())
  3. ->where("age > 18")
  4. ->group("age")
  5. ->order("age ASC");

having

  1. // SELECT count(1) number FROM `users` WHERE (age > 18) GROUP BY age HAVING (number >5) ORDER BY age ASC
  2. $select->from($this->getTableName(), "count(1) number")
  3. ->where("age > 18")
  4. ->group("age")
  5. ->having("number >5")
  6. ->order("age ASC");

连接查询(joinLeft、joinInner、joinRight)

  1. //SELECT u.*, d.`department_name` FROM `users` `u` LEFT JOIN `departments` `d` ON u.department_id = p.department_id WHERE (u.age > 18) ORDER BY u.age ASC
  2. $select->from($this->getTableName() . " AS u")
  3. ->where("u.age > 18")
  4. ->joinLeft($this->getTableName("departments") . " AS d", "u.department_id = p.department_id", array("department_name"))
  5. ->order("u.age ASC");

数量限制(limit、limitPage)

  1. //SELECT users.* FROM `users` WHERE (age > "18") ORDER BY age ASC LIMIT 20
  2. $select->from($this->getTableName())
  3. ->where("age > ?", 18)
  4. ->order("age ASC")
  5. ->limit(20);
  6. //SELECT users.* FROM `users` WHERE (age > "18") ORDER BY age ASC LIMIT 20 OFFSET 0
  7. $select->from($this->getTableName())
  8. ->where("age > ?", 18)
  9. ->order("age ASC")
  10. ->limitPage(1, 20);