Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

MySQL - Cách chuẩn hóa cột chứa các ID được phân tách bằng dấu phân cách

Khi tôi bắt đầu trả lời câu hỏi này, tôi nghĩ nó sẽ nhanh chóng và dễ dàng bởi vì tôi đã từng thực hiện một điều gì đó rất tương tự trong SQL Server, nhưng việc chứng minh khái niệm trong bản dịch đã phát triển thành giải pháp đầy đủ này.

Một lưu ý chưa rõ ràng trong câu hỏi của bạn là liệu bạn có điều kiện để khai báo id chính so với id bí danh hay không. Ví dụ:giải pháp này sẽ cho phép 1 có bí danh là 4 cũng như 4 có bí danh là 1, phù hợp với dữ liệu được cung cấp trong câu hỏi ví dụ đơn giản của bạn.

Để thiết lập dữ liệu cho ví dụ này, tôi đã sử dụng cấu trúc này:

CREATE TABLE notnormal_customers (
  id INT NOT NULL PRIMARY KEY,
  aliases VARCHAR(10)
);

INSERT INTO notnormal_customers (id,aliases)
VALUES
(1,'|4|58|76'),
(2,''),
(3,''),
(4,'|1|58|76'),
(58,'|1|4|76'),
(76,'|1|4|58');

Đầu tiên, để thể hiện mối quan hệ một-nhiều cho các bí danh từ một khách hàng đến nhiều, tôi đã tạo bảng này:

CREATE TABLE customer_aliases (
    primary_id INT NOT NULL,
    alias_id INT NOT NULL,
    FOREIGN KEY (primary_id) REFERENCES notnormal_customers(id),
    FOREIGN KEY (alias_id)   REFERENCES notnormal_customers(id),
    /* clustered primary key prevents duplicates */
    PRIMARY KEY (primary_id,alias_id)
)

Quan trọng nhất, chúng tôi sẽ sử dụng tùy chỉnh SPLIT_STR chức năng :

CREATE FUNCTION SPLIT_STR(
  x VARCHAR(255),
  delim VARCHAR(12),
  pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
       delim, '');

Sau đó, chúng tôi sẽ tạo một thủ tục được lưu trữ để thực hiện tất cả công việc. Mã được chú thích với các nhận xét cho nguồn tham chiếu.

DELIMITER $$
CREATE PROCEDURE normalize_customers()
BEGIN

  DECLARE cust_id INT DEFAULT 0;
  DECLARE al_id INT UNSIGNED DEFAULT 0;
  DECLARE alias_str VARCHAR(10) DEFAULT '';
  /* set the value of the string delimiter */
  DECLARE string_delim CHAR(1) DEFAULT '|';
  DECLARE count_aliases INT DEFAULT 0;
  DECLARE i INT DEFAULT 1;

  /*
    use cursor to iterate through all customer records
    http://burnignorance.com/mysql-tips/how-to-loop-through-a-result-set-in-mysql-strored-procedure/
  */
  DECLARE done INT DEFAULT 0;
  DECLARE cur CURSOR FOR
      SELECT `id`, `aliases`
      FROM `notnormal_customers`;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

  OPEN cur;
  read_loop: LOOP

    /*
      Fetch one record from CURSOR and set to customer id and alias string.
      If not found then `done` will be set to 1 by continue handler.
    */
    FETCH cur INTO cust_id, alias_str;
    IF done THEN
        /* If done set to 1 then exit the loop, else continue. */
        LEAVE read_loop;
    END IF;

    /* skip to next record if no aliases */
    IF alias_str = '' THEN
      ITERATE read_loop;
    END IF;

    /*
      get number of aliases
      https://pisceansheart.wordpress.com/2008/04/15/count-occurrence-of-character-in-a-string-using-mysql/
    */
    SET count_aliases = LENGTH(alias_str) - LENGTH(REPLACE(alias_str, string_delim, ''));

    /* strip off the first pipe to make it compatible with our SPLIT_STR function */
    SET alias_str = SUBSTR(alias_str, 2);

    /*
      iterate and get each alias from custom split string function
      https://stackoverflow.com/questions/18304857/split-delimited-string-value-into-rows
    */
    WHILE i <= count_aliases DO

      /* get the next alias id */
      SET al_id = CAST(SPLIT_STR(alias_str, string_delim, i) AS UNSIGNED);
      /* REPLACE existing values instead of insert to prevent errors on primary key */
      REPLACE INTO customer_aliases (primary_id,alias_id) VALUES (cust_id,al_id);
      SET i = i+1;

    END WHILE;
    SET i = 1;

  END LOOP;
  CLOSE cur;

END$$
DELIMITER ;

Cuối cùng, bạn chỉ cần chạy nó bằng cách gọi:

CALL normalize_customers();

Sau đó, bạn có thể kiểm tra dữ liệu trong bảng điều khiển:

mysql> select * from customer_aliases;
+------------+----------+
| primary_id | alias_id |
+------------+----------+
|          4 |        1 |
|         58 |        1 |
|         76 |        1 |
|          1 |        4 |
|         58 |        4 |
|         76 |        4 |
|          1 |       58 |
|          4 |       58 |
|         76 |       58 |
|          1 |       76 |
|          4 |       76 |
|         58 |       76 |
+------------+----------+
12 rows in set (0.00 sec)


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Tôi dường như không thể ngăn đầu vào tên trùng lặp

  2. Tại sao password_verify trả về false?

  3. EntityFramework 6 Alpha 2 &MySQL Connector / NET 6.6.4

  4. ĐỌC và VIẾT đồng thời trên Bảng MySQL

  5. quan hệ cha mẹ con cái