首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

ThinkPHP 查詢語句全攻略:字串/陣列/對象條件、關聯查詢、區間/組合/複合查詢實戰

     編輯:PHP 綜合知識

ThinkPHP 提供流暢的查詢構建器,支援字串、陣列、對象三種條件格式,以及關聯查詢、區間查詢、組合查詢、複合查詢等進階功能。本文系統整理各類查詢寫法,附帶實戰 SQL 對照與最佳實踐。

一、基礎條件格式

1. 字串條件(最直觀,防注入需自行處理)

$user = M('User');
$list = $user->where('id > 5 AND id < 9')->select();
// SQL: SELECT * FROM think_user WHERE id > 5 AND id < 9

2. 陣列條件(推薦,自動參數化,安全)

$map['username'] = 'john';
$map['status']   = 1;
$list = $user->where($map)->select();
// SQL: SELECT * FROM think_user WHERE username = 'john' AND status = 1

3. 對象條件(罕用,與陣列等效)

$cond = new stdClass();
$cond->username = 'john';
$list = $user->where($cond)->select();

二、進階查詢類型

區間查詢

$map['id'] = ['gt', 20];           // id > 20
$map['id'] = ['between', '10,20']; // 10 <= id <= 20
$map['id'] = [['gt', 20], ['lt', 30], 'and']; // 20 < id < 30
$map['id'] = [['gt', 20], ['lt', 30], 'or'];  // id > 20 OR id < 30

組合查詢

$map['username'] = 'john';
$map['password'] = ['eq', 'secret'];
$map['id']       = ['lt', 30];
$map['_logic']   = 'or'; // 所有上層條件 OR 關係
// WHERE username='john' OR password='secret' OR id<30

複合查詢

$where['username'] = ['eq', 'john'];
$where['password'] = ['like', 'p%'];
$where['_logic']   = 'or';

$map['_complex'] = $where;   // 子查詢群組
$map['id']       = ['lt', 30]; // 主條件 AND 子群組
// WHERE id < 30 AND (username='john' OR password LIKE 'p%')

三、關聯查詢

兩表 LEFT JOIN

$shops = M('Shops');
$list = $shops
    ->join('LEFT JOIN __PRODUCT__ AS p ON shops.product_id = p.id')
    ->field('p.id, p.name, shops.amount, shops.product_id')
    ->where("shops.user_id = {$_SESSION['user_id']}")
    ->group('shops.id')
    ->select();

三表 LEFT JOIN

$list = M('Shops')
    ->join('LEFT JOIN __PRODUCT__ p ON shops.product_id = p.id
            LEFT JOIN __PRODUCT_IMAGE__ pi ON pi.product_id = p.id')
    ->field('pi.url, p.id, p.name, shops.amount, shops.product_id,
             p.color, p.price, shops.amount * p.price AS total')
    ->where("shops.user_id = {$_SESSION['user_id']}")
    ->group('shops.id')
    ->select();

技巧:使用 __TABLE__ 佔位符自動替換表前綴,避免硬編碼。

四、查詢條件最佳實踐

場景推薦寫法理由
簡單等值/比較陣列 where(['col' => $val])自動參數化、防注入、可讀
複雜邏輯 (OR/AND 混合)組合/複合查詢陣列結構化表達邏輯,避免字串拼接
動態條件構建陣列累加 + _logic程式化控制,易擴展
原生 SQL 片段where('FIND_IN_SET(?, tags)', [$id])參數化原生片段,保留安全性
子查詢where('id IN (SELECT user_id FROM logs)')必要時使用,注意效能

五、常見錯誤與避坑

  1. 字串條件直接插入變數:where("name='$name'") → **SQL 注入**。改用 where(['name' => $name]) 或 where('name=?', [$name])。
  2. 關聯查詢欄位名衝突:兩表同名欄位(如 id)需用 field() 別名或 table.column 限定。
  3. group() 未包含非聚合欄位:MySQL ONLY_FULL_GROUP_BY 模式下會報錯,field() 僅選擇聚合或分組欄位。
  4. 陣列條件鍵名含運算子:where(['id>' => 5]) **無效**,需用 where(['id' => ['gt', 5]])。

總結

ThinkPHP 查詢構建器核心優勢在於**陣列條件自動參數化**與**複合查詢結構化表達**。實務原則:**優先用陣列條件,複雜邏輯用組合/複合查詢,關聯查詢善用 __TABLE__ 佔位符與 field() 別名,堅決杜絕字串拼接變數**。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved