开发者

Mysql - Stuck on a single row's value for all rows?

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜