簡體   English   中英

每當嘗試將另一個表中的列作為別名聯接時,出現“子查詢返回多個行”錯誤?

[英]Getting “subquery returns more than one row” error whenever trying to join a column from another table as alias?

我在整個問題上呆了整整2天。 我有一個users表,它包含:

+--------+--------+----------+--------------------------------------------+
| userId |  name  | username |          profile_pic                       |
+--------+--------+----------+--------------------------------------------+
|      1 | john   | john123  | http://localhost/profile_pic/user1pic.jpg  | 
|      2 | andrew | andrew   | http://localhost/profile_pi/user2pic.jpg   |
|      3 | doe    | doe      | http://localhost/profile_pic/user3pic.jpg  |
+--------+--------+----------+--------------------------------------------+

我還有另一個名為userpost表,其中包含:

+--------+--------+-------------+----------------------------+
| postId | userId | postMessage |         postImage          |
+--------+--------+-------------+----------------------------+
|      1 |      1 | "Hey"       | http://localhost/post1.jpg |
|      2 |      3 | "Add me"    | http://localhost/post2.jpg |
|      3 |      2 | "boring"    | http://localhost/post3.jpg |
+--------+--------+-------------+----------------------------+

userId users.userIdusers.userId 我正在嘗試將profile_pic加入到userpost但是mysql返回錯誤。 這是我在做什么:

 SELECT *, (SELECT profile_pic FROM users 
 INNER JOIN userpost on users.userId = userpost.userId) as profile_pic FROM userpost

但是獲取Subquery returns more than 1 row錯誤Subquery returns more than 1 row

我知道我在查詢中做一些愚蠢的事情。 我只想要這樣的東西:

 +--------+--------+-------------+----------------------------+--------------------------------------------+    
 | postId | userId | postMessage |         postImage          |    profile_pic                             |
 +--------+--------+-------------+----------------------------+--------------------------------------------+
 |      1 |      1 | "Hey"       | http://localhost/post1.jpg | http://localhost/profile_pic/user1pic.jpg  | 
 |      2 |      3 | "Add me"    | http://localhost/post2.jpg | http://localhost/profile_pic/user3pic.jpg  | 
 |      3 |      2 | "boring"    | http://localhost/post3.jpg | http://localhost/profile_pi/user2pic.jpg   |
 +--------+--------+-------------+----------------------------+--------------------------------------------+

我明天開會,展示我的原型應用程序。 幫助將不勝感激。

您正在使用子查詢而不是聯接。 在選擇中使用子查詢時,必須確保它返回的確是一行,例如

SELECT COL1,COL2,(SELECT 1) from YourTable

或通過使用相關查詢,我認為這是您的目的,但不是必需的,因為它來自與您選擇的表相同的表,所以只需使用簡單的聯接:

SELECT s.*, t.profile_pic
FROM users t 
INNER JOIN userpost s on t.userId = s.userId

嘗試這個...

SELECT *, 
 (SELECT top 1 profile_pic FROM users a
 where a.userId = b.userId order by b.postId desc) as profile_pic 
 FROM userpost b

但我不確定,以上查詢是否返回所需的個人資料圖片。

您沒有正確使用子查詢。應確保子查詢僅返回一行。

下面是您需要的查詢。

Select up.postId,u.userId,up.postMessage,up.postImage,u.profile_pic from user u
inner join userpost up on u.userId=up.userId

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM