简体   繁体   中英

My sub-query returns more than 1 row

Of course, the problem is that I'm getting more rows in the sub-query.

But I don't know how to fix it in order to achieve my goals.

This is how my query looks like:

SELECT movies.name, movies.id, 
    (SELECT username FROM users INNER JOIN movies ON 
        movies.added_by_id = users.id) 
    AS added_by_username FROM movies

For example we have more movies in our db, and those movies were added by users.

GOALS: I want to display all the movies and the usernames of the users who added them.

No need for a subquery:

SELECT movies.name, movies.id, username 
FROM users 
  INNER JOIN movies 
    ON movies.added_by_id = users.id
SELECT movies.name, movies.id, 
(SELECT TOP 1 username FROM users INNER JOIN movies ON 
    movies.added_by_id = users.id) 
AS added_by_username FROM movies

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM