Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
johnny-brav0 opened this issue Mar 7, 2022 · 2 comments
Open

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

johnny-brav0 opened this issue Mar 7, 2022 · 2 comments

Comments

@johnny-brav0
Copy link

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).

@Rufaid786
Copy link

This looks good

@lepiin
Copy link

lepiin commented Aug 4, 2023

UPDATE albums
SET release_year = 1986
WHERE release_year IS NULL;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants