mysql”;或“;查询操作的顺序


mysql "or" order of query operations

所以我使用的是laravel3,它在mysql数据库上使用了雄辩的ORM。我有一个函数可以反复往返于SQL server,而且我的测试用例有限,但我不能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();

我的问题是我不确定这里是如何工作的。它生成一个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运算符优先级

CCD_ 1具有比CCD_ 2更高的优先级。你的第一个例子和你的第二个例子是一样的。