Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL update with subquery and give error #1242 – Subquery returns more than 1 row

I have one question.
I have a table table_histories and look like this

id | id_personel | nip | created_at | updated at

and I have another table table_personels look like this

id | nip | name | created_at | updated_at

I want to update the table of table_histories look like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

UPDATE table_histories 
SET id_personel = (
    SELECT id 
    FROM table_personels 
    WHERE nip = table_histories.nip
)

and when I run it, it give me error

#1242 – Subquery returns more than 1 row.

one data on table_personels can have many histories.

any idea how to solve it?

>Solution :

Highest top 1 add this.

UPDATE table_histories 
SET id_personel = (
    SELECT id 
    FROM table_personels 
    WHERE nip = table_histories.nip
    ORDER BY id 
    LIMIT 1;
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading