peri hokiperihokiclarustologisticsrimashaop.compola taktis menang gates of olympus x1000 perihokipemain perihoki bongkar trik jepep starlight princess x1000perihoki beri panduan lengkap bermain mahjong ways 2 pgsofttips cara penghasil uang pgsoft mahjong ways 2 perihokimahjong wins 3 perihoki memberikan penggali terbesarfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76perihoki spin mahjong ways 2 pgsoft scatter hitam maxwinsimbol mahjong wins 3 yang sering muncul di awal perihokitrik gampang menang mahjong winscuan mahjong wins 3 tanpa hentipola spin taruhan kecil mahjong winsstrategi kunci scatter hitam beruntungacor total mahjong wayskemenangan maksimal mahjong wayskemenangan pria pekanbaru mahjong waysmahjong wins3 fenomena baru sumatera baratmenangkan mahjong dengan lincahmenembus batas keberuntungan mahjong waysmenjajal keberuntungan di mahjong wayspola gratis mahjong ways1scatter hitam senjata rahasia mahjong winsteknik spinning menguntungkan untuk scatter hitamanalisis rtp tertinggi mahjong winsfenomena scatter hitam mahjong winsibu rumah tangga dan keberuntungan mahjongkeseruan main mahjong wins3menang mahjong pengguna baru di sosial mediamenghargai setiap momen di mahjong waysserunya main mahjong di warung internetspin slow motion mancing scatterstrategi jitu maxwin mahjong waysstrategi jitu menang mahjong modal kecil jackpot besaranak kampung papua menggemparkan mahjongkeajaiban scatter hitam mahjong wayskeberhasilan joni mahjong winskejaiban mahjong ways di ujung jalanketua mahjong wins3 kehadiran kemenangan dimulai dari balimahjong kehadiran yang menguntungkanmahjong menggila adat88panduan kilat raih 108 jutatips memenangkan mahjongbarista kopi menang 110 juta spin mahjongdakota76 scatter hitam mahjong wins 3gagal cpns menang mahjong wins 3 rp120 jutakisah karyawan minimarket dapat thr lebih awal mahjong winskisah pelajar sma kuasai mahjong wins 3 inspirasi sekolahkisah penjaga parkir tanah abang motor baru mahjong winskisah sukses ibu rumah tangga bandung mahjong winsmahasiswa kkn beli iphone15 mahjong wins3 dakotamantan anak band influencer mahjong scatter hitampola rtp senin gates of olympus guru matematikatukang cuci motor menemukan keberuntungan di mahjong

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Phép tích đề cát trong MySQL - Freetuts
MYSQL CĂN BẢN
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Phép tích đề cát trong MySQL

Tích đề cát trong MySQL có nghĩa là truy vấn trên nhiều table trong cùng một câu SELECT và kêt quả trả về có thể là một trong số các fields của các table đó. Phép tích này ít khi được sử dụng trong MySQL mà thay vào đó sẽ sử dụng lệnh JOIN (sẽ học sau) để thay thế vì tốc độ query sẽ nhanh hơn.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Phép tích đề cát thường được dùng trong trường hợp hai table có mối quan hệ khóa ngoại (Foreign Key).

1. Tìm hiểu phép tích đề cát trong MySQL

Giả sử table A 50 recordstable B100 records thì kết quả trả về tối đa của phép tích này là 50 * 100 = 5000 records. Tại sao tôi lại nói là tối đa? Lý do là kết quả là bao nhiêu sẽ phụ thuộc vào mệnh đề WHERE nữa. Về tài nguyên cần để thực hiện cho phép tích khá là cao nên ta rất ít khi sử dụng nó, hầu như là không sử dụng luôn.

Để hiểu rõ hơn thì chúng ta làm một ví dụ nhé.

Bài viết này được đăng tại [free tuts .net]

Cho sơ đồ CSDL như sau:

phep-tich-de-cat.png

Trong sơ đồ này chúng ta có hai khóa chính và một khóa ngoại.

Dữ liệu bảng SINHVIEN:

data-bang-sinh-vien-tich-de-cat.png

Dữ liệu bảng LOP:

data-bang-lop-tich-de-cat.png

Bây giờ nếu chúng ta tích hai bảng này lại thì kết quả sẽ là 10 * 3 = 30 records. Nhưng nếu chúng ta truy vấn thêm điều kiện thì sẽ khác, ví dụ tích với điều kiện tên lớp là 'TOÁN' thì kết quả là 1 * 10 = 10 records như hình dưới đây.

result-tich-de-cat-php.png

Để ý kỹ hơn thì các bạn thấy khi chúng ta tích đề cát nếu SELECT * thì kết quả nó sẽ trả về tổng số các fields của hai bảng luôn. Ok bây giờ chúng ta tìm hiểu cú pháp và một vài ví dụ về câu SQL tích đề cát luôn nhé.

2. Cú pháp tích đề cát trong MySQL

Để tích hai bảng với nhau thì trong lệnh FROM chúng ta chỉ cần khai báo hai bảng cách nhau bởi dấu phẩy.

SELECT * 
FROM SINHVIEN, LOP

Như ở bài khóa ngoại tôi đã trình bày thì đối với sơ đồ trên bảng SINHVIEN gọi là bảng con và bảng LOP gọi là bảng cha. Mỗi SINHVIEN phải thuộc một LOP nào đó và mỗi LOP có thể có nhiều SINHVIEN.

Ví dụ 1: Lấy danh sách sinh viên thuộc lớp TOÁN thì ta phải làm thế nào?

Chúng ta sẽ sử dụng tích đề cát để nhân hai bảng lại với nhau, sau đó dựa vào kết quả của phép nhân này nó sẽ lọc theo điều kiện là TenLop = TOÁN. Trường hợp này chúng ta có

SELECT * 
FROM SINHVIEN, LOP
WHERE TenLOP = 'TOAN'

Ví dụ 2: Lấy danh sách sinh viên và tên lớp mà sinh viên đó đang học?

Để giải bài này thì chúng ta dựa vào khóa ngoại, nghĩa là trong hai bảng SINHVIEN và LOP nếu LopID bên bảng SINHVIEN = LopID trong bảng LOP thì kết quả đúng, câu SQL như sau:

SELECT * 
FROM SINHVIEN, LOP
WHERE SINHVIEN.LopID = LOP.LopID

Ví dụ 3: Lấy tên các sinh viên thuộc lớp CNTT

Bài này hơi khó vì chúng ta phải sử dụng hai điều kiện:

  • Để xác định sinh viên học lớp nào thì chúng ta sử dụng SINHVIEN.LopID = LOP.LopID
  • Nhưng để xác định tên lớp là CNTT nữa thì chúng ta phải thêm một điều kiện LOP.TenLop = 'CNTT'

SELECT SINHVIEN.TenSV
FROM SINHVIEN, LOP
WHERE SINHVIEN.LopID = LOP.LopID AND LOP.TenLop = 'CNTT'

Vậy ta có kết luận:

  • Sử dụng dấu phẩy để tích các bảng với nhau trong lệnh SELECT
  • Sử dụng lệnh WHERE để thiết lập điều kiện cho câu truy vấn trong phép tích đề cát.
  • Trong trường hợp hai bảng có tên column trùng nhau thì ban phải sử dụng cú pháp table_name.column để tránh xung đột. Như ví dụ trên thì LopID đã bị trùng cho cả hai bảng nên ở WHERE tôi có khai báo là SINHVIEN.LopID = LOP.LopID

3. Lời kết

Bài tích đề cát này là nền tảng để bạn học các kiến thức cao hơn như INNER JOIN, LEFT JOIN, RIGHT JOIN. Ở các bài đó bạn sẽ thấy chúng ta không nên sử dụng phép tích để cát để viết câu truy vấn. Bài này tôi sẽ dừng ở đây, chúc bạn học tốt.

Cùng chuyên mục:

Hàm ENCRYPT trong MySQL

Hàm ENCRYPT trong MySQL

Cách sử dụng ENCRYPT trong MySQL

Hàm MD5 trong MySQL

Hàm MD5 trong MySQL

Cách sử dụng MD5 trong MySQL

Hàm OLD_PASSWORD trong MySQL

Hàm OLD_PASSWORD trong MySQL

Cách sử dụng OLD_PASSWORD trong MySQL

Hàm PASSWORD trong MySQL

Hàm PASSWORD trong MySQL

Cách sử dụng PASSWORD trong MySQL

Hàm ASCII trong MySQL

Hàm ASCII trong MySQL

Cách sử dụng ASCII trong MySQL

Hàm CHAR_LENGTH trong MySQL

Hàm CHAR_LENGTH trong MySQL

Cách sử dụng CHAR_LENGTH trong MySQL

Hàm CHARACTER_LENGTH trong MySQL

Hàm CHARACTER_LENGTH trong MySQL

Cách sử dụng CHARACTER_LENGTH trong MySQL

Hàm CONCAT trong MySQL

Hàm CONCAT trong MySQL

Cách sử dụng CONCAT trong MySQL

Hàm CONCAT_WS trong MySQL

Hàm CONCAT_WS trong MySQL

Cách sử dụng CONCAT_WS trong MySQL

Hàm FIELD trong MySQL

Hàm FIELD trong MySQL

Cách sử dụng FIELD trong MySQL

Hàm FIND_IN_SET trong MySQL

Hàm FIND_IN_SET trong MySQL

Cách sử dụng FIND_IN_SET trong MySQL

Hàm FORMAT trong MySQL

Hàm FORMAT trong MySQL

Cách sử dụng FORMAT trong MySQL

Hàm INSERT trong MySQL

Hàm INSERT trong MySQL

Cách sử dụng INSERT trong MySQL

Hàm INSTR trong MySQL

Hàm INSTR trong MySQL

Cách sử dụng INSTR trong MySQL

Hàm LCASE trong MySQL

Hàm LCASE trong MySQL

Cách sử dụng LCASE trong MySQL

Hàm LEFT trong MySQL

Hàm LEFT trong MySQL

Cách sử dụng LEFT trong MySQL

Hàm LENGTH trong MySQL

Hàm LENGTH trong MySQL

Cách sử dụng LENGTH trong MySQL

Hàm LOCATE trong MySQL

Hàm LOCATE trong MySQL

Cách sử dụng LOCATE trong MySQL

Hàm LOWER trong MySQL

Hàm LOWER trong MySQL

Cách sử dụng LOWER trong MySQL

Hàm LPAD trong MySQL

Hàm LPAD trong MySQL

Cách sử dụng LPAD trong MySQL

Top