吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 790|回复: 0
收起左侧

[求助] 【gorm】下面的sql语句如何用gorm书写?

[复制链接]
thepoy 发表于 2020-8-1 13:49

三个表join,并且第一个表有查询条件,正确语句:

SELECT * FROM (SELECT b.*,u.username, bt.type, COUNT(c.id) as comments_count 
FROM blogs AS b
INNER JOIN users AS u ON b.user_id = u.id
INNER JOIN blog_types as bt ON b.blog_type_id = bt.id
LEFT JOIN comments AS c ON b.id = c.blog_id 
GROUP BY b.id) as r 
WHERE r.is_private = FALSE AND r.deleted_at IS NULL
ORDER BY click_count DESC

where语句中的两个条件其实是blogs中的字段,但我用blogs作为查询条件字段的表名时就报错,错误语句:

SELECT b.*,u.username, bt.type, COUNT(c.id) as comments_count 
FROM blogs AS b
INNER JOIN users AS u ON b.user_id = u.id
INNER JOIN blog_types as bt ON b.blog_type_id = bt.id
LEFT JOIN comments AS c ON b.id = c.blog_id 
GROUP BY b.id
WHERE b.is_private = FALSE AND b.deleted_at IS NULL

错误:

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 'WHERE b.is_private = FALSE AND b.deleted_at IS NULL' at line 7

如何使用gorm来表示上述语句?

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 13:29

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表