Mysql - 所有行都停留在单个行的值上?

发布于 2024-09-29 23:12:31 字数 520 浏览 0 评论 0原文

我遇到了 mysql 的问题,其中内部 select 语句始终使用外部表的第一行进行评估。在下面的代码中,“WHERE otherTable.someID = myTable1.table1_id”应该(在我看来)与最初选择的外部 myTable1.table1_id 匹配。也就是说,table1 的每一行都被处理,但内部 myTable1.table1_id 似乎总是卡在 table1 的第一行上,导致第一行的输出正确,但所有后续行的输出不正确。

就好像我用第一行 ID 的实际值替换了内部 myTable1.table1_id 并运行了该查询。

这是代码:

SELECT 
  myTable1.table1_id, 
  ..., 
  (SELECT otherStuff FROM otherTable WHERE otherTable.someID = myTable1.table1_id)
FROM table1 myTable1 
ORDER BY myTable1.table1_id;

感谢您的查看!

I am having an issue with mysql where an inner select statement is always being evaluated with the first row of the outer table. In the following code, "WHERE otherTable.someID = myTable1.table1_id" should (in my mind) match the outer myTable1.table1_id that is initially selected. That is, each row of table1 is processed, but the inner myTable1.table1_id seems to always be stuck on the first row of table1 resulting in correct output for the first row but incorrect for all subsequent rows.

Its as if I replaced the inner myTable1.table1_id with the actual value of the first row's ID and ran that query.

Here is the code:

SELECT 
  myTable1.table1_id, 
  ..., 
  (SELECT otherStuff FROM otherTable WHERE otherTable.someID = myTable1.table1_id)
FROM table1 myTable1 
ORDER BY myTable1.table1_id;

Thanks for looking!

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

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

发布评论

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

评论(1

萌面超妹 2024-10-06 23:12:31

看起来您正在尝试执行简单的联接:

SELECT 
  myTable1.table1_id, 
  ..., 
  otherstuff from othertable
FROM table1 myTable1 join othertable on myTable1.table1_id=othertable.some_ID
ORDER BY myTable1.table1_id;

如果您需要返回结果行,即使 othertable 不包含匹配的 ID,请将联接更改为左联接

Looks like you're trying to do a simple join:

SELECT 
  myTable1.table1_id, 
  ..., 
  otherstuff from othertable
FROM table1 myTable1 join othertable on myTable1.table1_id=othertable.some_ID
ORDER BY myTable1.table1_id;

If you need to get back result rows even when othertable doesn't contain a matching ID, change the join to a left join.

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