簡體   English   中英

mysql“或”查詢操作的順序

[英]mysql “or” order of query operations

所以我正在使用laravel 3,它在mysql數據庫上使用雄辯的ORM。 我有一個函數可以重復往返於SQL服務器,並且我已經使用了我的有限測試用例,但我不是100%確定sql查詢中的操作順序,所以我是希望有人能讓我知道我是否正確。 舊代碼如下

    $notifications = Notification::where(condition-1-a)->where(condition-1-b)->get();
    foreach ($locations = DB::table(table)->where(propertyX)->get() as $location) {
        $extra_notifications = Notification::where(condition-2-a)->where(condition-2-b)->get();
        $notifications = array_merge($notifications,$extra_notifications);
    }

我知道上面的代碼是有效的,並且保持隔離,因為每個查詢都是作為它自己的實體運行,然后結果被合並,但是一旦你開始獲得更多的位置,它需要花費很長的時間來運行。 我試圖通過只有一次sql服務器來減少這個,並提出了這個代碼。

    $notifications = Notification::where(condition-1-a)->where(condition-1-b);
    foreach ($locations = DB::table(table)->where(propertyX)->get() as $location) {
        $notifications = $notifications -> or_where(condition-2-a)->where(condition-2-b);
    }
$notifications = $notifications->get();

我的問題是我不確定or_where是如何工作的。 它生成一個sql字符串,其中包含我熟悉的單詞作為代碼,但我不確定它們是否會以相同的方式工作。 生成的字符串是

SELECT * FROM `notifications` 
    WHERE `condition-1-a` = ? AND `condition-1-b` = ? 
    OR `condition-2-a` = ? AND `condition-2-b` = ? 
    OR `condition-2-a` = ? AND `condition-2-b` = ? 
    OR `condition-2-a` = ? AND `condition-2-b` = ? 
    ... (etc depending on number of loop iterations)

我正在尋找的主要是確認查詢如何運行。 我可以期待像這樣跑嗎?

SELECT * FROM `notifications` 
    WHERE (`condition-1-a` = ? AND `condition-1-b` = ?) 
    OR (`condition-2-a` = ? AND `condition-2-b` = ? )
    OR (`condition-2-a` = ? AND `condition-2-b` = ? )
    OR (`condition-2-a` = ? AND `condition-2-b` = ? )
    ... [etc depending on number of loop iterations]

或者是否有一些其他順序來評估sql語句?

(我編寫了一些變量名稱和代碼的其他方面,我盡我所能將其歸結為每個人都可以使用的東西,以便它不是特定於我的情況)

MySQL運算符優先級

AND優先級高於OR 你的第一個例子與你的第二個例子相同。

暫無
暫無

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

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