MySQL 子查询在一张表中两次使用相同的 id 字段进行描述

发布于 2024-10-29 13:10:34 字数 556 浏览 8 评论 0原文

选择 move.idItem, item.description, ( SELECT location.location_name as movingFrom FROM move, location WHERE move.idlocationFrom = 位置.idlocation ) AS 移动自, ( SELECT location.location_name as movingTo FROM move, location WHERE move.idlocationTo = location.idlocation ) AS 移至 来自移动、项目 WHERE move.idItem = item.idItem

我试图使用上述查询获取移至和移自位置的名称,这会在 PHPMyAdmin 中产生“#1242 - 子查询返回超过 1 行”错误。

描述 项目移动存储在名为“移动”的表中。可以将项目从一个位置移动到另一位置,存储位置 ID(存储在“位置”表中的位置名称)和存储在“项目”表中的项目名称。 movingTo 和 movingFrom 将存储位置表中的 id。

有人可以帮我解决这个问题吗? 提前致谢

SELECT move.idItem, item.description,
(
SELECT location.location_name as movedFrom FROM move, location
WHERE move.idlocationFrom = location.idlocation
) AS movedFrom,
(
SELECT location.location_name as movedTo FROM move, location
WHERE move.idlocationTo = location.idlocation
) AS movedTo
FROM move , item
WHERE move.idItem = item.idItem

I'm trying to get the name of the location movedTo and movedFrom using the above query, which produdes '#1242 - Subquery returns more than 1 row' error in PHPMyAdmin.

Description
Item movements are stored in a table called 'move'. Items can be moved from one location to another storing the location id's (location names stored in 'location' table) and item names stored in 'item' table. movedTo and movedFrom will store the id's from the location table.

Can someone please help me with this query?
Thanks in advance

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

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

发布评论

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

评论(2

老旧海报 2024-11-05 13:10:34

我想你可能想使用连接而不是子查询。像这样的东西:

SELECT move.idItem, 
       item.description, 
       location1.location_name movedFrom, 
       location2.location_name as movedTo
FROM move 
INNER JOIN location location1 ON move.idlocationFrom = locatio1n.idlocation
INNER JOIN location location2 ON move.idlocationTo = location2.idlocation
INNER JOIN item ON move.idItem = item.idItem

I think you might want to use joins instead subqueries. Something like:

SELECT move.idItem, 
       item.description, 
       location1.location_name movedFrom, 
       location2.location_name as movedTo
FROM move 
INNER JOIN location location1 ON move.idlocationFrom = locatio1n.idlocation
INNER JOIN location location2 ON move.idlocationTo = location2.idlocation
INNER JOIN item ON move.idItem = item.idItem
太傻旳人生 2024-11-05 13:10:34
Table 1 -  cpe Table

|id | name
|----------
| 1 | cat
| 2 | dog
| 3 | mouse
| 4 | snake
-----------

Table 2 - AutoSelect

|id  | name   |  cpe1_id |  cpe2_id  | cpe3_id |
|-----------------------------------------------
| 1  | user1  |  1       |  3        | 4       |
| 2  | user2  |  3       |  1        | 2       |
| 3  | user3  |  3       |  3        | 2       |
| 4  | user4  |  4       |  2        | 1       |
------------------------------------------------

I would like to see an output of

user1 | cat   | mouse  |  snake  |
user2 | mouse | snake  |  dog    | 
..etc

 SELECT a.name, cpe1.name, cpe2.name, cpe3.name FROM AutoSelect as a
 LEFT JOIN cpe as cpe1 ON ( cpe1.id = a.cpe1_id ) 
 LEFT JOIN cpe as cpe2 ON ( cpe2.id = a.cpe2_id ) 
 LEFT JOIN cpe as cpe3 ON ( cpe3.id = a.cpe3_id )
Table 1 -  cpe Table

|id | name
|----------
| 1 | cat
| 2 | dog
| 3 | mouse
| 4 | snake
-----------

Table 2 - AutoSelect

|id  | name   |  cpe1_id |  cpe2_id  | cpe3_id |
|-----------------------------------------------
| 1  | user1  |  1       |  3        | 4       |
| 2  | user2  |  3       |  1        | 2       |
| 3  | user3  |  3       |  3        | 2       |
| 4  | user4  |  4       |  2        | 1       |
------------------------------------------------

I would like to see an output of

user1 | cat   | mouse  |  snake  |
user2 | mouse | snake  |  dog    | 
..etc

 SELECT a.name, cpe1.name, cpe2.name, cpe3.name FROM AutoSelect as a
 LEFT JOIN cpe as cpe1 ON ( cpe1.id = a.cpe1_id ) 
 LEFT JOIN cpe as cpe2 ON ( cpe2.id = a.cpe2_id ) 
 LEFT JOIN cpe as cpe3 ON ( cpe3.id = a.cpe3_id )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文