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

How to add a set of different values in one column(MySQL)?

Do not judge strictly, but I can not figure it out in any way
My table:

CREATE table courses  (id INT PRIMARY KEY AUTO_INCREMENT,
    -> faculty VARCHAR(55) NULL,
    -> number INT(10) NULL,
    -> diff VARCHAR(10) NULL);
mysql> select * from courses;

enter image description here

Target. Inject values (‘ez’, ‘mid’, ‘hard’) into diff column.
For exampl, im trying 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

mysql> INSERT courses (diff) VALUES ('ez');

OR

mysql> UPDATE courses SET faculty = 'chem', number = 2, diff = 'mid';

Add rows with empty id(values NULL).
PLZ help me!
I want to get this result

+----+---------+--------+------+
| id | faculty | number | diff |
+----+---------+--------+------+
|  1 | bio     |      1 | ez   |
|  2 | chem    |      2 | mid  |
|  3 | math    |      3 | hard |
|  4 | geo     |      4 | mid  |
|  5 | gum     |      5 | ez   |
+----+---------+--------+------+

>Solution :

You can use a case expression in an UPDATE statements:

UPDATE courses 
SET diff=CASE 
    WHEN faculty in ('bio', 'gum') THEN 'ez' 
    WHEN faculty in ('chem', 'geo') THEN 'mid' 
    WHEN faculty = 'math' THEN 'hard' 
    END;
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