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

Lệnh SELECT trong SQL Server - Freetuts
GETTING STARTED
DATA DEFINITION
DATA QUERIES
CONDITION & OPERATORS
EXPLORED
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Lệnh SELECT trong SQL Server

Trong bài viết này bạn sẽ được học cách sử dụng lệnh select trong SQL Server để thực hiện truy vấn trong một table, trường hợp truy vấn ở nhiều table thì sẽ được học ở bài khác.

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. Lệnh select trong SQL Server

Như bạn đã biết, dữ liệu sẽ được lưu trong table và mỗi table sẽ có nhiều cột thông tin khác nhau. Với nhu cầu chính của việc quản lý dữ liệu gồm 4 thao tác đó là: tìm kiếm, thêm, xóa và sửa thì lệnh select sẽ đáp ứng cho nhu cầu tìm kiếm.

Để tìm kiếm thì ta cần phải biết các thông tin như: tìm ở bảng nào, dữ liệu so sánh như thế nào, chọn ra các cột nào, ... Hãy xem CSDL mẫu dưới đây của bảng products.

sql server select customers table png

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

Trước tiên bạn hãy xem cú pháp của lệnh select đã nhé.

Cú pháp
SELECT
    select_list
FROM
    schema_name.table_name;

Trong đó:

  • select_list là tập hợp các column sẽ được chọn để hiển thị kết quả, các column được tách nhau bởi dấu phẩy.
  • schema_name là tên của lược đồ CSDL, như ở database mẫu thì ta có hai schema đó là: sales production.
  • table_name là tên của bảng muốn tìm kiếm.

Về thứ tự thực hiện thì hệ thống sẽ chạy từ FROM trở xuống rồi mới chạy đến SELECT, mặc dù lệnh select được viết ở đầu câu.

 2. Thực hành ví dụ với lệnh select trong SQL Server

Bây giờ ta sẽ truy vấn với bảng customers trong database mẫu nhé. Bảng này có các column như sau:

customers png

Ví dụ 1: Hãy lấy danh sách tất cả khách hàng và chỉ hiển thị firstname và lastname.

Áp dụng cú pháp thì ta sẽ viết câu truy vấn như sau:

SELECT
    first_name,
    last_name
FROM
    sales.customers;

Kết quả sẽ như sau:

sql server select some columns png

Bảng kết quả này ta gọi là tập hợp kết quả được trả về từ lệnh select. Giả sử bạn muốn lấy thêm cột email nữa thì câu SQL lúc này sẽ là:

SELECT
    first_name,
    last_name,
    email
FROM
    sales.customers;

Kết quả:

sql server select select three columns png

Ví dụ 2: Lấy danh sách khách hàng và hiển thị tất cả các thông tin có trong bảng.

Áp dụng ví dụ 1 thì ta sẽ lần lượt liệt kê từng column và cách nhau bởi dấu phẩy. Tuy nhiên có một cách khác đó là sử dụng dấu * để thay thế cho trường hợp chọn tất cả các column.

SELECT
    *
FROM
    sales.customers;

Kết quả như sau:

sql server select select all columns png

Dấu * rất tiện lợi trong trường hợp có quá nhiều column và bạn không thể nhớ hết tên column, hoặc viết ra thì quá dài dòng. Tuy nhiên khi làm thực tế bạn không nên lạm dụng dấu * quá bởi như vậy hệ thống sẽ chạy rất nặng vì dữ liệu sẽ bị dư thừa.  Ví dụ bạn xây dựng chức năng hiển thị da tên khách hàng thì chỉ cần viết select first_name, last_name là được rồi. Bạn cứ nghĩ nếu dữ liệu của bạn có 10GB thì khi chọn 1 phần nhỏ thôi sẽ nhẹ nhàng hơn là chọn cả 10GB.

3. Thêm điều kiện lọc với WHERE

Giả sử bạn cần lọc dữ liệu theo một điều kiện nào đó, ví dụ chọn những khách hàng đang sống tại Califorina (CA) thì ta sẽ viết câu truy vấn như sau:

SELECT
    *
FROM
    sales.customers
WHERE
    state = 'CA';

Chỉ có những dòng record nào có state = "CA" thì mới được chọn vào kết quả trả về.

sql server select where clause png

Điều này có nghĩa là khi bạn bổ sung lệnh WHERE thì chỉ có những dòng dữ liệu nào đáp ứng được lệnh điều kiện ở WHERE thì mới được chọn, đây là cách làm rất hay và rất quan trọng để bạn lọc dữ liệu trong thực tế, hầu như không thể thiếu.

Bạn có thể thêm nhiều điều kiện ở WHERE bằng cách sử dụng các toán tử như AND, OR. Ví dụ mình muốn lấy danh sách khách hàng sống tại CA và  city là Encino thì câu SQL lúc này sẽ như sau:

SELECT
    *
FROM
    sales.customers
WHERE
    state = 'CA'
    AND 
    CITY = 'Encino'

4. Sắp xếp kế quả trả về với ORDER BY

Đôi khi bạn muốn sắp xết kết quả trả về theo một quy luật tăng dần hoặc giảm dần thì hãy sử dụng lệnh ORDER BY.

Chúng ta có hai cách sắp xếp:

  • DESC là sắp xếp giảm dần theo bảng mã ASCII
  • ASC là sắp xếp tăng dần theo bảng mã ASCII

Ví dụ: Hãy lấy danh sách khách hàng đang sống ở CA và sắp xếp tên khách hàng theo bảng chữ cái.

SELECT
    *
FROM
    sales.customers
WHERE
    state = 'CA'
ORDER BY
    first_name ASC;

Kết quả:

sql server select order by clause png

Mặc định nếu bạn không chỉ định ASC hay DESC thì SQL Server sẽ lấy ASC.

Trên là những kiến thức căn bản về lệnh select trong SQL Server, đây là lệnh rất quan trọng và được sư dụng rất nhiều trong việc truy xuất và tìm kiếm dữ liệu.

Cùng chuyên mục:

Hàm CHAR trong SQL Server

Hàm CHAR trong SQL Server

Cách sử dụng hàm CHAR trong SQL Server

Hàm CHARINDEX trong SQL Server

Hàm CHARINDEX trong SQL Server

Cách sử dụng hàm CHARINDEX trong SQL Server

Hàm CONCAT trong SQL Server

Hàm CONCAT trong SQL Server

Cách sử dụng hàm CONCAT trong SQL Server

Hàm + trong SQL Server

Hàm + trong SQL Server

Cách sử dụng hàm + trong SQL Server

Hàm DATALENGTH trong SQL Server

Hàm DATALENGTH trong SQL Server

Cách sử dụng hàm DATALENGTH trong SQL Server

Hàm LEFT trong SQL Server

Hàm LEFT trong SQL Server

Cách sử dụng hàm LEFT trong SQL Server

Hàm LEN trong SQL Server

Hàm LEN trong SQL Server

Cách sử dụng hàm LEN trong SQL Server

Hàm LOWER trong SQL Server

Hàm LOWER trong SQL Server

Cách sử dụng hàm LOWER trong SQL Server

Hàm LTRIM trong SQL Server

Hàm LTRIM trong SQL Server

Cách sử dụng hàm LTRIM trong SQL Server

Hàm NCHAR trong SQL Server

Hàm NCHAR trong SQL Server

Cách sử dụng hàm NCHAR trong SQL Server

Hàm PATINDEX trong SQL Server

Hàm PATINDEX trong SQL Server

Cách sử dụng hàm PATINDEX trong SQL Server

Hàm REPLACE trong SQL Server

Hàm REPLACE trong SQL Server

Cách sử dụng hàm REPLACE trong SQL Server

Hàm RIGHT trong SQL Server

Hàm RIGHT trong SQL Server

Cách sử dụng hàm RIGHT trong SQL Server

Hàm RTRIM trong SQL Server

Hàm RTRIM trong SQL Server

Cách sử dụng hàm RTRIM trong SQL Server

Hàm SPACE trong SQL Server

Hàm SPACE trong SQL Server

Cách sử dụng hàm SPACE trong SQL Server

Hàm STR trong SQL Server

Hàm STR trong SQL Server

Cách sử dụng hàm STR trong SQL Server

Hàm UPPER trong SQL Server

Hàm UPPER trong SQL Server

Cách sử dụng hàm UPPER trong SQL Server

Hàm SUBSTRING trong SQL Server

Hàm SUBSTRING trong SQL Server

Cách sử dụng hàm SUBSTRING trong SQL Server

Hàm STUFF trong SQL Server

Hàm STUFF trong SQL Server

Cách sử dụng hàm STUFF trong SQL Server

Hàm ABS trong SQL Server

Hàm ABS trong SQL Server

Cách sử dụng hàm ABS trong SQL Server

Top