简体   繁体   English

mysql“或”查询操作的顺序

[英]mysql “or” order of query operations

So I'm working with laravel 3 which use eloquent ORM on a mysql database. 所以我正在使用laravel 3,它在mysql数据库上使用雄辩的ORM。 I've got a function that makes repeated round trips to the SQL server, and I've got it working with my limited test cases, but I'm not 100% sure about the order of operations in an sql query, so I was hoping someone could give me an idea of if I am correct. 我有一个函数可以重复往返于SQL服务器,并且我已经使用了我的有限测试用例,但我不是100%确定sql查询中的操作顺序,所以我是希望有人能让我知道我是否正确。 The old code is as follows 旧代码如下

    $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);
    }

I know the above code works, and keeps things isolated, because each query is run as it's own entity, and then the results are merged, but once you start getting more locations it takes a looong time to run. 我知道上面的代码是有效的,并且保持隔离,因为每个查询都是作为它自己的实体运行,然后结果被合并,但是一旦你开始获得更多的位置,它需要花费很长的时间来运行。 I'm trying to reduce this by having only one trip to the sql server, and have come up with this code. 我试图通过只有一次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();

My problem here is that I'm not sure how or_where works. 我的问题是我不确定or_where是如何工作的。 It generates an sql string with words I'm familiar with as a code, but I'm not sure they will work the same way. 它生成一个sql字符串,其中包含我熟悉的单词作为代码,但我不确定它们是否会以相同的方式工作。 The generated string is 生成的字符串是

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)

The main thing I'm looking for is confirmation of how the query runs. 我正在寻找的主要是确认查询如何运行。 Can I expect to run like this? 我可以期待像这样跑吗?

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]

Or is there some other order in which sql statments are evaluated? 或者是否有一些其他顺序来评估sql语句?

(I redacted some variable names and other aspects of the code, I did my best to boil it down to something everyone can use so that it's not specific to my situation) (我编写了一些变量名称和代码的其他方面,我尽我所能将其归结为每个人都可以使用的东西,以便它不是特定于我的情况)

MySQL operator precedence MySQL运算符优先级

AND has higher precedence than OR . AND优先级高于OR Your first example is the same as your second. 你的第一个例子与你的第二个例子相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM