Mysql chọn kết hợp hai cột

Bài viết này sẽ hướng dẫn bạn nhiều cách để nối dữ liệu của hai cột trong

INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
4. Nhiệm vụ này có thể dễ dàng được thực hiện theo lập trình bằng cách chọn các trường từ bảng
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
4 và lưu trữ các giá trị của chúng vào một biến sau khi nối các giá trị của chúng

Có thể đơn giản hóa quy trình trên bằng cách nối các giá trị khi bạn chọn các hàng từ Bảng cơ sở dữ liệu

Bây giờ chúng ta lấy một ví dụ đơn giản. Nếu bạn có hai cột riêng biệt trong cơ sở dữ liệu là tên và họ và muốn hiển thị giá trị trong cả hai cột dưới dạng một chuỗi tên đầy đủ, bạn có thể sử dụng một trong hai cách tiếp cận đã cho để hoàn thành nhiệm vụ của mình. Tương tự, nếu một địa chỉ được chia thành nhiều cột trong cơ sở dữ liệu và bạn muốn nó là một địa chỉ duy nhất, bao gồm thành phố, tiểu bang và quốc gia trong ứng dụng của mình, thì hàm

INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
6 sẽ rất hữu ích

Hai chức năng khác nhau có thể được sử dụng để hoàn thành nhiệm vụ này

  1. Sử dụng hàm
    INSERT INTO student 
    VALUES (111,'Sim','Marlw','[email protected]'),
    (124,'John','Carl','[email protected]'),
    (362,'Paul','cohelo','[email protected]'),
    (244,'Lunas','sen','[email protected]');
    
    INSERT INTO student 
    VALUES (114,'Jaine','Kora','[email protected]'),
    (615,'Roma','Sholy','[email protected]'),
    (997,'Beaon','shrlon','[email protected]'),
    (332,'Peter','cohelo','[email protected]');
    
    6
  2. Sử dụng hàm
    INSERT INTO student 
    VALUES (111,'Sim','Marlw','[email protected]'),
    (124,'John','Carl','[email protected]'),
    (362,'Paul','cohelo','[email protected]'),
    (244,'Lunas','sen','[email protected]');
    
    INSERT INTO student 
    VALUES (114,'Jaine','Kora','[email protected]'),
    (615,'Roma','Sholy','[email protected]'),
    (997,'Beaon','shrlon','[email protected]'),
    (332,'Peter','cohelo','[email protected]');
    
    0

Cả hai hàm

INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
1 và
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
2 đều nối hai hoặc nhiều chuỗi. Hai hàm này khác nhau vì hàm
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
2 hoạt động cùng với dấu phân cách giữa các chuỗi, trong khi hàm
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
1 không cho phép sử dụng dấu phân cách. Có một sự khác biệt đáng kể khác giữa các hàm này ở chỗ hàm
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
1 trả về giá trị
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
6 nếu bất kỳ đối số nào là
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
6. Ngược lại, hàm
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
2 chỉ trả về giá trị
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
6 nếu dấu phân cách là
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
6

Đoạn script sau tạo một bảng

INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
11 với bốn cột (
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
12,
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
13,
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
14 và
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
15)

INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
6

Ở đây, chúng tôi chèn các giá trị dữ liệu mẫu vào bảng sinh viên để trình diễn

INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');

Truy vấn đã cho bên dưới trả về tất cả dữ liệu từ bảng sinh viên

INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
1

Mysql chọn kết hợp hai cột

Nối các cột bằng hàm
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
6 trong MySQL

Hàm

INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
6 có thể nối hoặc kết hợp các giá trị từ nhiều cột thành một cột. Cú pháp của hàm
INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
6 như hình bên dưới

INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
9

Bây giờ chúng ta sẽ xem cách sử dụng điều này với truy vấn

INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
19

INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
1

đầu ra sẽ là

concatenate two columns in mysql - using concat

INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
2

Bảng gốc không được cập nhật. Có thể sử dụng truy vấn

INSERT INTO student 
VALUES (111,'Sim','Marlw','[email protected]'),
(124,'John','Carl','[email protected]'),
(362,'Paul','cohelo','[email protected]'),
(244,'Lunas','sen','[email protected]');

INSERT INTO student 
VALUES (114,'Jaine','Kora','[email protected]'),
(615,'Roma','Sholy','[email protected]'),
(997,'Beaon','shrlon','[email protected]'),
(332,'Peter','cohelo','[email protected]');
90 để hiển thị nội dung trên dấu nhắc