db.Model("? as u", db.Model("users").Fields("name", "age")).Where("age", 18).Scan(&users)
// SELECT * FROM (SELECT `name`,`age` FROM `users`) as u WHERE `age` = 18
subQuery1 := db.Model("users").Fields("name")
subQuery2 := db.Model("pets").Fields("name")
db.Model("? as u, ? as p", subQuery1, subQuery2).Scan(&users)
// SELECT * FROM (SELECT `name` FROM `users`) as u, (SELECT `name` FROM `pets`) as p
6 Comments
xx
有没EXISTS的子查询?比如像这个查询某日新增付费用户数的sql语句
SELECT COUNT(1) FROM payment_order p WHERE created_at between '2021-06-09 00:00:00' and '2021-06-09 23:59:59' AND `status`=1 AND NOT EXISTS (SELECT DISTINCT uid from payment_order pp WHERE created_at < "2021-06-09 00:00:00" AND p.uid=pp.uid )
郭强
暂时没有快速方法,你可以这样:
xx
谢谢大佬已经可以了,之前用dao.PaymentOrder.Where一直子查询报错,用g.Model("paument_order",“p”).Where就可以了;
不过大佬写的WhereLT函数好像1.6还没有
不告诉你
有没有
And
和Or
多条件查询的组合查询?比如
where (a=1 or b=1) and c= 1
这种,在 Laravel 里面可以用闭包传入 where 实现 ,试了下,没有找到好的解决方案写了子查询
subQuery := g.Model("test").Where("a", 1).WhereOr("b", 1) g.Model("test").Where(subQuery).Where("c", 1)
实际子查询没有生效
小棠
你可以采用 Wheref 进行格式化查询语句。
Fred_li
``
使用join和from子查询,有报错,但是可以正常返回结果 这是什么原因呢
2021-12-22 15:08:07.169 [ERRO] [ 49 ms] [default] SHOW FULL COLUMNS FROM
Error: Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
'' at line 1