简体   繁体   中英

MYSQL - how to set multiple values in same column and row mysql

I am trying to insert several values to a row in same column. for instance: where ID = 5 to set in column1 values: 4,7,9 but not as a string.

you calso store it in a SET

Table

CREATE TABLE `jj` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `s` set('1','2','3') DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

and insert like this :

INSERT INTO `jj` (`id`, `s`)
VALUES
    (3, '2'),
    (4, '2,3');

sample

MariaDB [l]> SELECT * FROM jj;
+----+------+
| id | s    |
+----+------+
|  1 | 2    |
|  2 | 2    |
|  3 | 2    |
|  4 | 2,3  |
+----+------+
4 rows in set (0.00 sec)

MariaDB [l]> INSERT INTO `jj` (`id`, `s`)
    -> VALUES
    ->     (5, '1,3'),
    ->     (6, '1,2');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [l]> SELECT * FROM jj;
+----+------+
| id | s    |
+----+------+
|  1 | 2    |
|  2 | 2    |
|  3 | 2    |
|  4 | 2,3  |
|  5 | 1,3  |
|  6 | 1,2  |
+----+------+
6 rows in set (0.00 sec)

MariaDB [l]>

Change the design of you table to:

ID |  Column1
---+----------
5  |    4
5  |    7
5  |    9

Then you can select with:

SELECT * FROM table WHERE ID = 5

to get all your values.

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