dqxafj6830 2018-10-23 22:30
浏览 1758
已采纳

gorm用OR查询

I'm stuck in generating a query which is dynamically created in the run-time.

I want to create a having query with OR in the middle so e.g.

SELECT name FROM `user_group`  WHERE ((group_key = 'age' AND group_value = '20')) 

OR ((group_key = 'division' AND group_value = 'accounting')) 
OR ((group_key = 'age' AND group_value = '22')) 
OR ((group_key = 'division' AND group_value = 'kitchen'))

GROUP_BY name
HAVING 
((SUM(group_key = 'age' AND group_value = '20') > 0) 
AND 
(SUM(group_key = 'division' AND group_value = 'accounting') > 0))

OR 

((SUM(group_key = 'age' AND group_value = '22') > 0) 
AND 
(SUM(group_key = 'division' AND group_value = 'kitchen') > 0))

Note that the OR inside having statement is what I'm asking.

I got this with gorm currently:

SELECT name FROM `user_group`  WHERE ((group_key = 'age' AND group_value = '20')) 

OR ((group_key = 'division' AND group_value = 'accounting')) 
OR ((group_key = 'age' AND group_value = '22')) 
OR ((group_key = 'division' AND group_value = 'kitchen'))

GROUP_BY name
HAVING 
((SUM(group_key = 'age' AND group_value = '20') > 0) 
AND 
(SUM(group_key = 'division' AND group_value = 'accounting') > 0))

AND 

((SUM(group_key = 'age' AND group_value = '22') > 0) 
AND 
(SUM(group_key = 'division' AND group_value = 'kitchen') > 0))

Note the AND in the having statement

This is the query generation:

for _, condition := range resp.Allow.Conditions {
    for key, val := range condition {
        if len(key) <= 0 || len(val) <= 0 {
            continue
        }
        groupQuery = groupQuery.Or("(group_key = ? AND group_value = ?)", key, val)
        groupQuery = groupQuery.Having("SUM(group_key = ? AND group_value = ?) > 0", key, val)
    }
}
groupQuery = groupQuery.Group('name')

Is there any method to do this in gorm? I've looked at the documentation and my best bet that it has to be a raw sql query. I don't prefer it but if it's the only way than it's ok.

NB: I'm using mysql as the dialect

  • 写回答

1条回答 默认 最新

  • douxiajia6309 2018-10-24 04:34
    关注

    The output of the line:

        groupQuery = groupQuery.Having("SUM(group_key = ? AND group_value = ?) > 0", key, val)
    

    is the block

    ((SUM(group_key = 'age' AND group_value = '20') > 0) 
    AND 
    (SUM(group_key = 'division' AND group_value = 'accounting') > 0))
    
    AND 
    
    ((SUM(group_key = 'age' AND group_value = '22') > 0) 
    AND 
    (SUM(group_key = 'division' AND group_value = 'kitchen') > 0))
    

    Which is correct. Look at all the opening and closing brackets per line:

    ````(COND1) AND (COND2) AND (COND3) AND (COND4)```

    TO get a single or in the middle of the having statement as you request:

    ((SUM(group_key = 'age' AND group_value = '20') > 0) 
    AND 
    (SUM(group_key = 'division' AND group_value = 'accounting') > 0))
    
    AND 
    
    ((SUM(group_key = 'age' AND group_value = '22') > 0) 
    AND 
    (SUM(group_key = 'division' AND group_value = 'kitchen') > 0))
    

    which would be:

    (COND1) AND (COND2) OR (COND3) AND COND(4)

    would lead to a less expected result.

    More logical would be to have:

    (COND1) OR (COND2) OR (COND3) OR COND(4)

    Or:

    ((COND1) AND (COND2)) OR ((COND3) AND COND(4))

    This last version (which seems to be your target), can not be generated in a loop as stated, and would require a specific approach.

    It looks like you are pretty much relegated to just raw SQL for this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加