簡體   English   中英

在SQL中選擇查詢而不在數據庫中檢索值

[英]Select query in sql not retrieving the value in database

如果我只有一個帶有integer employee_id (例如1001 ),我會得到答案

像這樣的代碼示例

   $claimprocess = Employee::find()
                    ->where("employee_id = '1004'  and importcompany_id = 1")
                    ->andwhere(" status != 'Deleted' and relationship = 'Self'")
                    ->all();

如果我有一個結合使用intergercharacteremployee_id ,則無法得到答案(例如E1004 ),

如下示例代碼,

$claimprocess = Employee::find()
                    ->where("employee_id = 'E1004'  and importcompany_id = 1")
                    ->andwhere(" status != 'Deleted' and relationship = 'Self'")
                    ->all();

當我執行此代碼時,出現如下錯誤

帶有消息“ SQLSTATE [42S22]”的異常(數據庫異常)“ yii \\ db \\ Exception”:找不到列:1054 Champ'E1004'inconnu dans where子句正在執行的SQL是:SELECT * FROM employee WHERE(employee_id = E1004和importcompany_id = 1)AND(狀態!='已刪除')'

更新:

實際上是從另一個變量獲取值(E1004),我使用變量而不是值,出於理解的目的,我在問題中使用了一個值

您需要將employee_id值括起來,即E1004引號引起來,因為它包含字符串文字。 所以您的查詢看起來像

->where("employee_id = 'E1004'  and importcompany_id = 1")

SQL中的字符串文字用單引號( ' )表示。 沒有它們,數據庫將把E1004解釋為列名,並導致查詢失敗,因為您的表沒有這樣的查詢。

$claimprocess = Employee::find()
                ->where("employee_id = 'E1004'  and importcompany_id = 1")
                ->andwhere(" status != 'Deleted' and relationship = 'Self'")
                ->all();

您需要正確傳遞值,就像現在您沒有正確傳遞值一樣,因此Yii無法正確生成Sql查詢。 您可以將第二個參數傳遞給where子句

    $claimprocess = Employee::find()
                        ->where("employee_id = ':employee_id'  and importcompany_id = 1" , array(':employee_id'=>'E1004'))
                        ->andwhere(" status != 'Deleted' and relationship = 'Self'")
                        ->all();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM