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','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
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','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
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úngCó 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','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
6 sẽ rất hữu íchHai chức năng khác nhau có thể được sử dụng để hoàn thành nhiệm vụ này
- Sử dụng hàm
6INSERT INTO student VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'], [124,'John','Carl','John.carl@gmail.com'], [362,'Paul','cohelo','paul.coh@google.com'], [244,'Lunas','sen','Lonas.sen@max.com']; INSERT INTO student VALUES [114,'Jaine','Kora','jaine.kora@abs.com'], [615,'Roma','Sholy','roma.sh11@yahoo.com'], [997,'Beaon','shrlon','beatrice.ss22@yahoo.com'], [332,'Peter','cohelo','peter.coh@google.com'];
- Sử dụng hàm
0INSERT INTO student VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'], [124,'John','Carl','John.carl@gmail.com'], [362,'Paul','cohelo','paul.coh@google.com'], [244,'Lunas','sen','Lonas.sen@max.com']; INSERT INTO student VALUES [114,'Jaine','Kora','jaine.kora@abs.com'], [615,'Roma','Sholy','roma.sh11@yahoo.com'], [997,'Beaon','shrlon','beatrice.ss22@yahoo.com'], [332,'Peter','cohelo','peter.coh@google.com'];
Cả hai hàm
INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
1 và INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
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','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
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','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
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','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
1 trả về giá trị INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
6 nếu bất kỳ đối số nào là INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
6. Ngược lại, hàm INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
2 chỉ trả về giá trị INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
6 nếu dấu phân cách là INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
6Đoạn script sau tạo một bảng
INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
11 với bốn cột [INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
12, INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
13, INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
14 và INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
15]INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
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','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
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','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
1Nối các cột bằng hàm INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
6 trong MySQL
INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
Hàm
INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
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','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
6 như hình bên dướiINSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
9Bâ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','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
19INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
1đầu ra sẽ là
INSERT INTO student
VALUES [111,'Sim','Marlw','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
2Bả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','sim.marlw@gmail.com'],
[124,'John','Carl','John.carl@gmail.com'],
[362,'Paul','cohelo','paul.coh@google.com'],
[244,'Lunas','sen','Lonas.sen@max.com'];
INSERT INTO student
VALUES [114,'Jaine','Kora','jaine.kora@abs.com'],
[615,'Roma','Sholy','roma.sh11@yahoo.com'],
[997,'Beaon','shrlon','beatrice.ss22@yahoo.com'],
[332,'Peter','cohelo','peter.coh@google.com'];
90 để hiển thị nội dung trên dấu nhắc