这个 UNION ALL mysql 查询有什么问题?

发布于 2024-10-26 10:59:49 字数 567 浏览 3 评论 0原文

SELECT 'Q' AS TYPE, 
       q.question AS value, 
       q.date
  FROM questions q
 WHERE q.user_id =39
UNION ALL 
SELECT 'A' AS TYPE, 
       q.question AS value, 
       a.date
  FROM answers a,
       questions q
 WHERE a.question_id = q.id
   AND WHERE a.user_id =39
ORDER BY `date` DESC

数据库设计:

  • 问题{id,user_id,问题,日期}
  • 答案{id,question_id,user_id,答案,日期}

错误:

1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 'WHERE a . user_id = 39 ORDER BY 日期 DESC

SELECT 'Q' AS TYPE, 
       q.question AS value, 
       q.date
  FROM questions q
 WHERE q.user_id =39
UNION ALL 
SELECT 'A' AS TYPE, 
       q.question AS value, 
       a.date
  FROM answers a,
       questions q
 WHERE a.question_id = q.id
   AND WHERE a.user_id =39
ORDER BY `date` DESC

database design:

  • questions{id,user_id,question,date}
  • answers{id,question_id,user_id,answer,date}

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 'WHERE a . user_id = 39 ORDER BY date DESC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青衫负雪 2024-11-02 10:59:49

查询的下部有两个 WHERE 子句。您需要从字面上删除最后一个单词 WHERE。错误描述就是您所需要的。

There are two WHERE-clauses in the lower part of the query. You need to literally remove just the last word WHERE. The error description is all you need.

墨洒年华 2024-11-02 10:59:49

“WHERE”出现两次。其中 x = ..... 且 ....

"WHERE" appears twice. Where x = ..... AND ....

娇妻 2024-11-02 10:59:49

有一个额外的地方 a.user_id=39。您仅在子句开头使用关键字 WHERE (a.question_id = q.id AND a.user_id = 39)

SELECT 
'Q' AS TYPE , q.question AS value, q.date
FROM questions q
WHERE q.user_id =39
UNION ALL 
SELECT 'A' AS TYPE , q.question AS value, a.date
FROM answers a,questions q
WHERE a.question_id = q.id
AND
a.user_id =39
ORDER BY `date` DESC

Had an extra where a.user_id=39. You only use the keyword WHERE at the beginning of the clause (a.question_id = q.id AND a.user_id = 39)

SELECT 
'Q' AS TYPE , q.question AS value, q.date
FROM questions q
WHERE q.user_id =39
UNION ALL 
SELECT 'A' AS TYPE , q.question AS value, a.date
FROM answers a,questions q
WHERE a.question_id = q.id
AND
a.user_id =39
ORDER BY `date` DESC
七禾 2024-11-02 10:59:49

正如错误试图解释的那样:对于第二个选择,您只需要输入一个“WHERE”。

SELECT 'A' AS TYPE , q.question AS value, a.date
FROM answers a,questions q
WHERE a.question_id = q.id
AND
a.user_id =39

As the error tries to explain: For the second select you only need to put one "WHERE".

SELECT 'A' AS TYPE , q.question AS value, a.date
FROM answers a,questions q
WHERE a.question_id = q.id
AND
a.user_id =39
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文