如何使用yii查询构建器编写此SQL查询
我有这个SQL查询,它正在执行我想要的工作:
SELECT `Table1`.* FROM `Table1`
LEFT JOIN `Table2` ON `Table1`.`idTable2` = `Table2`.`id`
WHERE (`Table1`.`idOwner`=156 AND `Table2`.`enabled`=1 AND day(Table2.creationDate) <= 5 AND date_format(Table2.creationDate, '%Y-%m') = '2022-12')
OR (`Table1`.`idOwner`=156 AND `Table2`.`enabled`=1 AND date_format(Table2.creationDate, '%Y-%m') != '2023-01' AND date_format(Table2.creationDate, '%Y-%m') != '2022-12')
我尝试将其复制为yii查询构建器,喜欢这样:
Table1::find()
->joinWith(['table2'])
->where(['Table1.idOwner' => $idOwner, 'Table2.enabled' => 1])
->andWhere(['<=', 'day(Table2.creationDate)', $expirationDay])
->andWhere(['=', "date_format(Table2.creationDate, '%Y-%m')", $previousExpirationMonthYear])
->orWhere(['Table1.idOwner' => $idOwner, 'Table2.enabled' => 1])
->andWhere(['!=', "date_format(Table2.creationDate, '%Y-%m')", $currentExpirationMonthYear])
->andWhere(['!=', "date_format(Table2.creationDate, '%Y-%m')", $previousExpirationMonthYear])
->all();
但是我打印了SQL此查询构建器以getrawsql()
生成,并且它返回了这个奇怪的方式:
SELECT `Table1`.* FROM `Table1`
LEFT JOIN `Table2` ON `Table1`.`idTable2` = `Table2`.`id`
WHERE (((((((`Table1`.`idOwner`=156)
AND (`Table2`.`enabled`=1))
AND (day(Table2.creationDate) <= 5))
AND (date_format(Table2.creationDate, '%Y-%m') = '2022-12'))
OR ((`Table1`.`idOwner`=156)
AND (`Table2`.`enabled`=1)))
AND (date_format(Table2.creationDate, '%Y-%m') != '2023-01'))
AND (date_format(Table2.creationDate, '%Y-%m') != '2022-12'))
AND (`Table1`.`idOwner`='156')
对不起,如果很难以这种方式阅读。
谁能像我想要的那样帮助我制作查询构建器吗?我会非常感谢
I have this SQL query which is doing what I want:
SELECT `Table1`.* FROM `Table1`
LEFT JOIN `Table2` ON `Table1`.`idTable2` = `Table2`.`id`
WHERE (`Table1`.`idOwner`=156 AND `Table2`.`enabled`=1 AND day(Table2.creationDate) <= 5 AND date_format(Table2.creationDate, '%Y-%m') = '2022-12')
OR (`Table1`.`idOwner`=156 AND `Table2`.`enabled`=1 AND date_format(Table2.creationDate, '%Y-%m') != '2023-01' AND date_format(Table2.creationDate, '%Y-%m') != '2022-12')
I tried replicating it as a Yii Query Builder like that:
Table1::find()
->joinWith(['table2'])
->where(['Table1.idOwner' => $idOwner, 'Table2.enabled' => 1])
->andWhere(['<=', 'day(Table2.creationDate)', $expirationDay])
->andWhere(['=', "date_format(Table2.creationDate, '%Y-%m')", $previousExpirationMonthYear])
->orWhere(['Table1.idOwner' => $idOwner, 'Table2.enabled' => 1])
->andWhere(['!=', "date_format(Table2.creationDate, '%Y-%m')", $currentExpirationMonthYear])
->andWhere(['!=', "date_format(Table2.creationDate, '%Y-%m')", $previousExpirationMonthYear])
->all();
But I printed the SQL this Query Builder generates with getRawSql()
and it returns in this strange way:
SELECT `Table1`.* FROM `Table1`
LEFT JOIN `Table2` ON `Table1`.`idTable2` = `Table2`.`id`
WHERE (((((((`Table1`.`idOwner`=156)
AND (`Table2`.`enabled`=1))
AND (day(Table2.creationDate) <= 5))
AND (date_format(Table2.creationDate, '%Y-%m') = '2022-12'))
OR ((`Table1`.`idOwner`=156)
AND (`Table2`.`enabled`=1)))
AND (date_format(Table2.creationDate, '%Y-%m') != '2023-01'))
AND (date_format(Table2.creationDate, '%Y-%m') != '2022-12'))
AND (`Table1`.`idOwner`='156')
Sorry if its difficult to read it that way.
Can anyone help me to make the Query Builder like the way I want? I would be very appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当您使用
and Where()
或or Where()
查询构建器采取现有条件并执行类似的事情时:or
respectively.
So if you already have some complex condition and then you try to call
You will get
但是在您的情况下,您需要得到类似的东西:
To get something like that you can build the first complex condition same way but you have to build second condition in one
orWhere()
call.或使其更可读性,您可以单个呼叫构建两个复杂条件:Whenever you use
andWhere()
ororWhere()
the query builder takes existing conditions and do something like this:or
respectively.
So if you already have some complex condition and then you try to call
You will get
But in your case you need to get something like:
To get something like that you can build the first complex condition same way but you have to build second condition in one
orWhere()
call. Or to make it more readable you can build both complex conditions each in single call: