gpt4 book ai didi

mysql - 将内部连接转换为左侧连接

转载 作者:行者123 更新时间:2023-11-30 22:25:49 26 4
gpt4 key购买 nike

我有一个 rake 文件,上面有这段代码。 .

   if circle[:conference2_total_channels].present?
conf = Confbridge.find(:all, :conditions => ["confbridges.cid = ?",circle.circle_number],
:joins => [:confbridge_participants],
:select => 'confbridges.id, confbridges.confno, confbridges.max_member, (confbridges.max_member - 5) AS additional , (confbridges.max_member - IFNULL(confbridge_participants.active,0)) AS vacant')

end

代码的返回是

SELECT confbridges.id, confbridges.confno, confbridges.max_member, (confbridges.max_member - 5) AS additional , (confbridges.max_member - IFNULL(confbridge_participants.active,0)) AS vacant FROM `confbridges` **INNER JOIN** `confbridge_participants` ON `confbridge_participants`.`confbridge_id` = `confbridges`.`id` WHERE (confbridges.cid = '0090000092')

它是INNER JOIN

如何将其更改为LEFT JOIN

像这样

SELECT confbridges.id, confbridges.confno, confbridges.max_member, (confbridges.max_member - 5) AS additional , (confbridges.max_member - IFNULL(confbridge_participants.active,0)) AS vacant FROM `confbridges` **LEFT JOIN** `confbridge_participants` ON `confbridge_participants`.`confbridge_id` = `confbridges`.`id` WHERE (confbridges.cid = '0090000092')

谢谢 :)

最佳答案

我会这样写:

Confbridge.where("confbridges.cid = ?",circle.circle_number)
.joins("LEFT JOIN confbridge_participants
ON confbridge_participants.confbridge_id = confbridges.id")
.select(...your select statement)

关于mysql - 将内部连接转换为左侧连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35266548/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com