Mysql - 所有行都停留在单个行的值上?
我遇到了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在尝试执行简单的联接:
如果您需要返回结果行,即使 othertable 不包含匹配的 ID,请将联接更改为
左联接
。Looks like you're trying to do a simple join:
If you need to get back result rows even when othertable doesn't contain a matching ID, change the join to a
left join
.