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 matematikaidola scatter hitam gampang meledakidola sensasi jackpotidola pola scatter hitamidola hasilkan jackpot dalam waktu singkatidola mahjong wins 3 bocortukang 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

Mệnh đề WHERE trong SQL - Freetuts
TABLE QUERIES
DATA QUERIES
CONDITION & OPERATOR
ADVANCED
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Mệnh đề WHERE trong SQL

Ở bài trước chúng ta đã học cách sử dụng lệnh SELECT để lấy dữ liệu từ bảng, nhưng lệnh đó sẽ lấy tất cả dữ liệu hiện có, vì vậy trong bài này mình giới thiệu thêm một lệnh khác đó là lệnh WHERE dùng để lọc dữ liệu từ kết quả trả về đó.

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.

1. Tìm hiểu Lệnh Where trong T-SQL

Lệnh WHERE được dùng để kết hợp trong lệnh SELECT và vị trí được đặt sau lệnh FROM để lọc dữ liệu, cú pháp như sau:

Cú pháp
SELECT columns_list
FROM table_name 
WHERE conditions

Ví dụ
SELECT *
FROM customers 
WHERE age = 32

Trong ví dụ này thì age = 32 chính là condition, ý nghĩa của nó là lọc qua tất cả dữ liệu trong bảng và chọn dòng nào có age = 32.

Bạn có thể sử dụng các toán tử so sánh như: 

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

  • > lớn hơn
  • < bé hơn
  • = bằng
  • >= lớn hơn hoặc bằng
  • <= bé hơn hoặc bằng
  • LIKE so sánh chuỗi trùng khớp
  • NOT so sánh không giống, thường kết hơp với LIKE, IN, ..

Trong danh sách này có thể có một số toán tử bạn chưa biết đến, tuy nhiên đừng lo lắng quá nhé, mình sẽ giới thiệu ở các bài tiếp theo.

2. Ví dụ với WHERE trong T-SQL

Giả sử ta có bảng dữ liệu như sau (bảng này được dùng cho toàn bộ các phần phía dưới đây):

ID  NAME       AGE       ADDRESS            SALARY 
1   Ramesh     32        Ahmedabad          2000.00 
2   Khilan     25        Delhi              1500.00 
3   kaushik    23        Kota               2000.00 
4   Chaitali   25        Mumbai             6500.00 
5   Hardik     27        Bhopal             8500.00 
6   Komal      22        MP                 4500.00 
7   Muffy      24        Indore             10000.00

Bây giờ mình cần truy vấn lấy các columns ID, Name and Salary của những khách hàng nào có mức lương lớn hơn 3000.

SELECT ID, NAME, SALARY  
FROM CUSTOMERS 
WHERE SALARY > 3000;

Kết quả sẽ trả về như sau:

ID  NAME       SALARY 
4   Chaitali   6500.00 
5   Hardik     8500.00 
6   Komal      4500.00 
7   Muffy      10000.00

Một yêu cầu khác như sau, lấy khách hàng có tên là "Hardik".

SELECT ID, NAME, SALARY  
FROM CUSTOMERS 
WHERE NAME = 'Hardik';

Kết quả trả về như sau:

ID  NAME     SALARY 
5   Hardik   8500.00 

3. Where với hơn một điều kiện

Nếu bạn muốn truy vấn với nhiều hơn một điều kiện thì sao? Lúc này bạn phải sử dụng đến hai toán tử thông dụng nhất là OR hoặc AND.

  • AND sẽ trả về TRUE nếu cả hai vế phải và vế trái đều đúng, ngược lại một trong hai vế sai thì sẽ trả về FALSE
  • OR  sẽ trả về TRUE nếu ít nhất một trong hai vế phải và trái đúng, và trả về FALSE nếu cả hai vế đều sai.

Cú pháp
SELECT columns_name
FROM table_name
WHERE condition1 [AND][OR] condition2 

Ví dụ: Lấy danh sách khách hàng có salary > 4500ID > 4.

SELECT ID, NAME, SALARY  
FROM CUSTOMERS 
WHERE SALARY > 4500 AND ID > 4;

Kết quả như sau:

ID  NAME       AGE       ADDRESS            SALARY 
5   Hardik     27        Bhopal             8500.00 
7   Muffy      24        Indore             10000.00 

Ví dụ: Lấy danh sách khách hàng có salary > 6000 hoặc age >= 25.

SELECT ID, NAME, SALARY  
FROM CUSTOMERS 
WHERE SALARY < 6000 AND AGE >= 25;

Kết quả như sau:

ID  NAME       AGE       ADDRESS            SALARY 
2   Khilan     25        Delhi              1500.00 
4   Chaitali   25        Mumbai             6500.00 
5   Hardik     27        Bhopal             8500.00 
7   Muffy      24        Indore             10000.00 

Trên là ví dụ về cách sử dụng lệnh WhERE trong SQL, bạn có thể áp dụng nó với hầu hết các hệ quản trị CSDL hiện nay.

Cùng chuyên mục:

Phiên làm việc là gì trong SQL

Phiên làm việc là gì trong SQL

Trong bài viết này mình sẽ giải thích phiên làm việc là gì trong SQL…

Ràng buộc Check trong SQL

Ràng buộc Check trong SQL

Bài này bạn sẽ được học cách tạo ràng buộc giá trị trong SQL bằng…

Khóa ngoại Foreign Key trong SQL

Khóa ngoại Foreign Key trong SQL

Trong bài này bạn sẽ được học cách tạo khóa ngoại Foreign Key bằng câu…

Primary Key trong SQL

Primary Key trong SQL

Bài này bạn sẽ được giới thiệu cách tạo khóa chính (Primary Key) thể hiện…

Create và Drop Database trong SQL

Create và Drop Database trong SQL

Bài viết này sẽ hướng dẫn bạn cách sử dụng lệnh Create Database để tạo…

Subquery trong SQL - truy vấn con

Subquery trong SQL - truy vấn con

Trong bài này mình sẽ nói đến một cách sử dụng câu truy vấn SLECT…

Self Join trong SQL

Self Join trong SQL

Mỗi phép Join đều phải có hai bảng table1 và table2, tuy nhiên có trường…

Right Join trong SQL

Right Join trong SQL

Chào các bạn, hôm nay mình sẽ tiếp tục nói đến phép Join trong SQL.…

Left Join trong SQL

Left Join trong SQL

Ở bài trước chúng ta đã được tìm hiểu Inner Join và biết được tại…

Inner JOIN trong SQL

Inner JOIN trong SQL

Ở bài trước chúng ta đã học hiểu cách truy vấn nhiều bảng trong SQL…

Toán tử BETWEEN và NOT BETWEEN trong SQL

Toán tử BETWEEN và NOT BETWEEN trong SQL

Toán tử BETWEEN dùng để kiểm tra một giá trị có nằm trong một khoảng…

Tìm hiểu ALIAS  (AS) trong SQL

Tìm hiểu ALIAS (AS) trong SQL

Trong bài này mình sẽ nói về cách đặt lại tên cho column, table bằng…

Tích đề các trong SQL

Tích đề các trong SQL

Trong bài này mình sẽ hướng dẫn các bạn cách truy vấn dữ liệu trên…

Gộp dữ liệu với UNION và UNION ALL trong SQL

Gộp dữ liệu với UNION và UNION ALL trong SQL

Có một số trường hợp bắt buộc bạn phải chạy hai câu truy vấn nên…

Lọc dữ liệu trùng với DISTINCT trong SQL

Lọc dữ liệu trùng với DISTINCT trong SQL

Đôi khi có những câu truy vấn có kết quả bị trùng lặp thì lúc…

Toán tử AND và OR trong SQL

Toán tử AND và OR trong SQL

Trong bài này chúng ta học hai toán tử có thể nói là được sử…

Toán tử IN và NOT IN trong SQL

Toán tử IN và NOT IN trong SQL

Trong bài này chúng ta sẽ học hai lệnh mới dùng để kiểm tra một…

Toán tử LIKE và NOT LIKE trong SQL

Toán tử LIKE và NOT LIKE trong SQL

Trong bài này chúng ta sẽ tìm hiểu lệnh LIKE, lệnh này dùng để so…

Sắp xếp dữ liệu với ORDER BY trong SQL

Sắp xếp dữ liệu với ORDER BY trong SQL

Nếu bạn muốn sắp xếp dữ liệu trả về theo tăng dần hoặc giảm dần…

Lệnh Delete trong SQL

Lệnh Delete trong SQL

DELETE FROM posts WHERE post_id > 7000 AND post_content IS NOT NULL;

Top