原则:多个(whereIn OR whereIn)查询?

发布于 2024-08-23 11:27:21 字数 527 浏览 5 评论 0原文

我在使用 Doctrine 制作一个相当简单的查询时遇到了麻烦...

我有两个数组($countries、$cities),我需要检查数据库记录值是否与其中任何一个数组匹配。我正在寻找类似的内容:

->whereIn('country', 'city', $countries, $cities)

...“国家”是 $countries 的 WHERE IN,“city”是 $city 的 WHERE IN。

我可以将两者分开,但所需的查询有很多其他条件,所以这是不可能的。我所追求的结果 SQL 是:

SELECT ... 
WHERE ... 
AND ...
AND ... 
AND ('country' IN (1,2,3) OR 'city' IN (7,8,9))
AND ... 
AND ...;

因此,人们也可以将其视为一个括号问题。有人知道 Doctrine DQL 是否可以实现这一点?我查看了文档但找不到任何方向。

谢谢

I'm having trouble crafting a fairly simple query with Doctrine...

I have two arrays ($countries, $cities) and I need to check whether database record values would match any inside either. I'm looking for something like:

->whereIn('country', 'city', $countries, $cities)

... with 'country' being a WHERE IN for $countries and 'city' being a WHERE IN for $city.

I could separate the two out but the needed query has lots of other conditions so that's not possible. The resulting SQL I'm after would be:

SELECT ... 
WHERE ... 
AND ...
AND ... 
AND ('country' IN (1,2,3) OR 'city' IN (7,8,9))
AND ... 
AND ...;

One could therefore think of it also as a bracketing issue only. Anyone know if this is possible with Doctrine DQL? I've looked through the documentation but can't find any direction.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴我老 2024-08-30 11:27:21

经过一小时的实验,下面是让它发挥作用的语法。

$q->andWhere('country IN ? OR city IN ?', array(array(1, 2, 3), array(7, 8, 9)));

After an hour of experimenting on this nonsense, here's the syntax to make it work.

$q->andWhere('country IN ? OR city IN ?', array(array(1, 2, 3), array(7, 8, 9)));
尾戒 2024-08-30 11:27:21

为什么不使用类似的东西?

$countryIds=[1,2,3];
$cityIds=[7,8,9];

$q->whereIn('country',$countryIds)->andWhereIn('city',$cityIds);

另外,将它们链接在一起以获取上下文(大多数 Doctrine 方法返回 $this)。

请参阅http://www.symfony-project.org/学说/1_2/en/06-处理数据

Why not use something like?

$countryIds=[1,2,3];
$cityIds=[7,8,9];

$q->whereIn('country',$countryIds)->andWhereIn('city',$cityIds);

Also, chain them together for context (most Doctrine methods return $this).

see http://www.symfony-project.org/doctrine/1_2/en/06-Working-With-Data

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文