MySQL 查询:从两个表中检索信息,而无需执行两次查询

发布于 2024-08-12 07:02:48 字数 340 浏览 4 评论 0原文

我的数据库中有两个表,我想从它们中检索信息而不必执行两次查询。基本上,需要使用从任务表​​检索的 user_ID 从用户表中获取相应的用户名。这是我到目前为止所拥有的,但查询返回 false:

SELECT t.user_id, t.nursery_ss, t.nursery_ws, t.greeter, t.date
   u.user_first_name, u.user_last_name
   FROM tasks_tbl AS t
   INNER JOIN users_tbl AS u ON t.user_id = u.user_id
   WHERE t.date = '2009-11-29'

I have two tables in my database, and I would like to retrieve information from both of them without having to do two queries. Basically, the user_ID(s) retrieved from the tasks table needs to be used to get those respective user(s) names from the users table. This is what I have so far, but the query is returning false:

SELECT t.user_id, t.nursery_ss, t.nursery_ws, t.greeter, t.date
   u.user_first_name, u.user_last_name
   FROM tasks_tbl AS t
   INNER JOIN users_tbl AS u ON t.user_id = u.user_id
   WHERE t.date = '2009-11-29'

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

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

发布评论

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

评论(2

坏尐絯 2024-08-19 07:02:48

您忘记了选择列表中 t.date 之后的逗号:

SELECT t.user_id, t.nursery_ss, t.nursery_ws, t.greeter, t.date -- comma needed here
   u.user_first_name, u.user_last_name
FROM tasks_tbl AS t
INNER JOIN users_tbl AS u ON t.user_id = u.user_id
WHERE t.date = '2009-11-29'

You forgot a comma after t.date in your select-list:

SELECT t.user_id, t.nursery_ss, t.nursery_ws, t.greeter, t.date -- comma needed here
   u.user_first_name, u.user_last_name
FROM tasks_tbl AS t
INNER JOIN users_tbl AS u ON t.user_id = u.user_id
WHERE t.date = '2009-11-29'
旧城空念 2024-08-19 07:02:48

事实上,你的查询看起来不错;您可以尝试检查查询的其他方面是否有错误。例如,尝试仅从指定的联接中执行 SELECT *。或者尝试省略 WHERE 子句;您将获得比您想要的更多的数据,但它可以帮助您调试查询。

Actually, your query looks good; you might try checking to see if other aspects of the query are faulty. For example, try just a SELECT * from your join as specified. Or try omitting your WHERE clause; you'll get more data than you want, but it may help you debug your query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文