FleaPHP【數據庫】 查詢條件($conditions) 的寫法

 

FleaPHP中,凡是用到數據庫查詢的函數,都需要查詢條件參數$conditions,現講述用法如下:

舉例:

  1. // $conditions 保存<b style=”color: black; background-color: rgb(153, 255, 153);”>查詢</b>條件
  2. $conditions = ‘level_ix > 1′;
  3. // $tableOrders 是一個訂單數據表的表數據入口對象
  4. $order = $tableOrders->find($conditions, ‘created DESC’, ‘id, title, body’);
  5. $conditions = array(‘username’ => ‘dualface’);
  6. // $tableUsers 是一個用戶信息數據表的表數據入口對象
  7. $user = $tableUsers->find($conditions);
  // $conditions 保存查詢條件
  $conditions = 'level_ix > 1';
  // $tableOrders 是一個訂單數據表的表數據入口對象
  $order = $tableOrders->find($conditions, 'created DESC', 'id, title, body');
  $conditions = array('username' => 'dualface');
  // $tableUsers 是一個用戶信息數據表的表數據入口對象
  $user = $tableUsers->find($conditions);

$conditions 參數可以是整數字符串數組三種類型:

1.如果 $conditions 參數是一個整數,則假定該整數爲主鍵字段值。

  1. // <b style=”color: black; background-color: rgb(153, 255, 153);”>查詢</b>主鍵字段值爲1的記錄
  2. $user = $tableUsers->find(1);
  3. // 如果主鍵字段名爲“id”,則生成的where字句爲“WHERE `id` = 1”
// 查詢主鍵字段值爲1的記錄
$user = $tableUsers->find(1);
// 如果主鍵字段名爲“id”,則生成的where字句爲“WHERE `id` = 1”

2.如果 $conditions 參數是一個字符串,則該字符串將直接作爲查詢條件,這種方式可以支持最靈活的查詢條件。 例如:

  1. $conditions = ‘id < 3′
  2. $user = $tableUsers->find($conditions);
  3. //生成的where字句爲“WHERE id < 3”
$conditions = 'id < 3'
$user = $tableUsers->find($conditions);
//生成的where字句爲“WHERE id < 3”

3.1.如果 $conditions 參數是一個數組,且指定了鍵名和值,則查詢條件中字段名爲鍵名,字段值等於鍵值。例如:

  1. // <b style=”color: black; background-color: rgb(153, 255, 153);”>查詢</b>id字段值爲3的記錄
  2. $conditions = array(
  3. ‘id’ => ‘1′,
  4. );
  5. $user = $tableUsers->find($conditions);
  6. //生成的where字句爲“WHERE `id` = 1”
// 查詢id字段值爲3的記錄
$conditions = array(
   'id' => '1',
  );
$user = $tableUsers->find($conditions);
//生成的where字句爲“WHERE `id` = 1”

3.2.如果 $conditions 參數是一個數組,但其中的元素沒有鍵名, 則假定鍵值爲自定義查詢條件,例如

  1. $conditions = array(‘id = 1′);
  2. // 生成的where字句爲“WHERE `id` = 1”
  3. $user = $tableUsers->find($conditions);
$conditions = array('id = 1');
// 生成的where字句爲“WHERE `id` = 1”
$user = $tableUsers->find($conditions);

3.3.$conditions 爲數組時,可以混用字符串和鍵值對兩種風格:

  1. $conditions = array(
  2. ‘id < 3′,
  3. ’sex’ => ‘male’,
  4. );
  5. $user = $tableUsers->find($conditions);
  6. // 生成的where字句爲“id < 3 AND `sex` = ‘male’”
$conditions = array(
   'id < 3',
   'sex' => 'male',
);
$user = $tableUsers->find($conditions);
// 生成的where字句爲“id < 3 AND `sex` = 'male'”

$conditions 爲數組時,多個查詢條件之間將使用 AND 布爾運算符進行連接。

3.4.“in()”查詢FleaPHP中的實現。(原文由DreamPig發表於http://www.fleaphp.org/bbs/viewthread.php?tid=2168
我們有時候要用到in這樣的操作,那麼在condition裏面怎麼寫呢?

  1. // 假如主鍵名爲“id”,需要<b style=”color: black; background-color: rgb(153, 255, 153);”>查詢</b>id的值爲1、2、3其中之一,則可以這樣寫:
  2. $condition = array(
  3. ‘in()’ => array(1,2,3),
  4. )
  5. $user = $tableUsers->find($conditions);
  6. // 生成的where子句爲“WHERE `id` IN (1, 2, 3)”
// 假如主鍵名爲“id”,需要查詢id的值爲1、2、3其中之一,則可以這樣寫:
$condition = array(
    'in()' => array(1,2,3),
)
$user = $tableUsers->find($conditions);
// 生成的where子句爲“WHERE `id` IN (1, 2, 3)”

那麼如果不是主鍵的話怎麼寫了呢? 也很簡單,提供鍵值對即可。例如:

  1. $condition = array(
  2. ‘in()’ => array(
  3. ‘username’ => array(‘username1′,‘username2′)
  4. )
  5. )
  6. $user = $tableUsers->find($conditions);
  7. // 生成的where子句爲“WHERE `username` IN (’username1′, ‘username2′)”
$condition = array(
    'in()' => array(
                    'username' => array('username1','username2')
                 )
    )
$user = $tableUsers->find($conditions);
// 生成的where子句爲“WHERE `username` IN ('username1', 'username2')”

4.find()函數中其它參數的含義和用法如下:

4.1.$sort 參數指定查詢時的排序方式,類型只能爲字符串
例如 ‘created ASC’ 表示按照“created”字段進行從小到大的排序。

4.2.$fields 參數指定查詢結果中要包含哪些字段,類型可以爲字符串或數組
當數據表的字段很多時,通過指定 $fields 參數可以避免查詢不需要的字段,從而提高性能。
$fields 參數即可是以“,”逗號分隔的字段名,也可以是包含多個字段名的數組,例如:

  1. $fields = array(‘title’, ‘created’);
  2. //也可以寫成下面的字符串形式,兩種寫法作用相同,區別在於自動生成的字段名兩邊將會添加上“`”符號,以防止出現字段名與SQL關鍵字衝突的情況出現。建議手寫時也加上“`”字符
  3. $fields = ‘title, created’;
  4. $user = $tableUsers->find(‘id < 10′,NULL,$fields);
$fields = array('title', 'created');
//也可以寫成下面的字符串形式,兩種寫法作用相同,區別在於自動生成的字段名兩邊將會添加上“`”符號,以防止出現字段名與SQL關鍵字衝突的情況出現。建議手寫時也加上“`”字符
$fields = 'title, created';
$user = $tableUsers->find('id < 10',NULL,$fields);

推薦使用數組,這樣表數據入口處理起來更快一些。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章