Skip to content

Alternate solution to Exercise 7 (Not an Issue) #6

Open
@johnny-brav0

Description

@johnny-brav0

First off, thanks for creating this repo. It really has helped me a lot in getting started with SQL.
Now as posted in the solutions for exercise 7, the solution, I feel, is quite static. So, I searched about the error, Error Code: 1093. You can't specify target table 'albums' for update in FROM clause I got while trying to solve it in a single query. The following solution works dynamically:

UPDATE albums
SET release_year = 1986
WHERE albums.id = (
    SELECT id
    FROM (SELECT a.id
        FROM albums AS a
        WHERE release_year IS NULL) AS c
);

Upon reading this article I found that the above used to be a complex query in earlier versions of MYSQL which involved creating a temporary table. But starting from version 5.6 onward it has been optimised. So, I assume it is safe to use.
Also, coming from python background, this feels quite satisfying to do the task in one line (query in this case).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions