SELECT DATE(lastModifiedAt) AS targetdate, LEFT(userId, 8) AS idpserviceid, COUNT…
- User
SELECT DATE(lastModifiedAt) AS target_date, LEFT(userId, 8) AS idp_service_id, COUNT (DISTINCT userId) AS sign_up_count FROM oauth_approvals WHERE lastModifiedAt >= '2021-10-16 00:00:00' AND lastModifiedAt <= '2023-11-30 23:59:59' AND clientServiceId = 'LT001001' AND LEFT (userId, 8) IN ('TH001001', 'QA001001', 'IT002002', 'BR002001') GROUP BY target_date, idp_service_id ORDER BY target_date ASC 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 'DISTINCT userId) AS sign_up_count FROM oauth_approvals WHERE lastMod' at line 4 时间: 0.13s sql的错误信息 请问我sql的问题在哪
- Assistant
在您的SQL查询中,您尝试使用
COUNT(DISTINCT userId)的方法来计算不同的userId总数,但似乎存在语法错误。错误信息指出问题出现在COUNT (DISTINCT userId)的使用上,但在此上下文中,该使用看起来是正确的。一个可能的错误是在
COUNT函数后有一个多余的空格。在SQL中,函数名称和括号之间通常不应该有空格。尽管在许多SQL数据库中允许这种用法,但有些版本可能对此敏感。尝试去掉COUNT和左括号之间的空格,像这样:sqlSELECT DATE(lastModifiedAt) AS target_date, LEFT(userId, 8) AS idp_service_id, COUNT(DISTINCT userId) AS sign_up_count FROM oauth_approvals WHERE lastModifiedAt >= '2021-10-16 00:00:00' AND lastModifiedAt <= '2023-11-30 23:59:59' AND clientServiceId = 'LT001001' AND LEFT(userId, 8) IN ('TH001001', 'QA001001', 'IT002002', 'BR002001') GROUP BY target_date, idp_service_id ORDER BY target_date ASC;请注意,在
LEFT(userId, 8)条件中指定的值都是8个字符长的,这意味着LEFT(userId, 8)调用将始终返回的前8个字符。因此,如果您的是更长的字符串,则上述语句应该正常工作。