peri hokiperihokiclarustologisticsrimashaop.comangelpaintingtools.co.iningin cuan cepat joki profesional gates of olympus perihokijalan perihoki pasti cuan dari spin scatter hitam mahjong ways 2 pgsoftmahjong wins 3 sukses dengan teknik pancingan gacor perihkibanyak jalan alternatif baccarat online di perihokikemenangan substansial mahjong ways 2 keuntungan nyata perihokiscatter hitam mahjong wins 3 spin perihoki pasti jeperahasia rtp live pola pragmatic gates of olympus duta76trik duta7 pgsoft mahjong ways 2 modal receh maxwingampang meraih keberuntungan di mahjong wins 3 duta76cara dapat saldo lewat jalur pintas dadu sicbo duta76spin mahjong ways 2 pgsoft scatter hitam maxwin duta76duta76 berikan kemenangan konsisten di mahjong wins 3mahjong wins bantu ekonomi keluargaabc1131 bahas rtp live mahjong pgsoftmomen menarik mahjong wins yang harus anda ketahuipesona scatter wild mahjong ways2spin pelan mahjong ways penghasil bonus harianstrategi unik kakek royteknik memancing scatter hitam untuk meraih cuaninovasi mahjong wins untuk pemain barumenggugah dewa naga mahjong ways munculkan scatter hitammenikmati pola mahjong ways sambil bersantaimeningkatkan peluang dengan jeda spin mahjong wayspeluang kaya dalam scatter wild

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ấy bài viết ngẫu nhiên trong MySQL (Random Records)
VIEW
STORED PROCEDURE
INDEX
FULL TEXT SEARCH
TRIGGER
TIPS
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Lấy bài viết ngẫu nhiên trong MySQL (Random Records)

Trong bài viết này mình sẽ hướng dẫn cách lấy dữ liệu ngâu nhiên trong MySQL, đây là chức năng thường gặp khi bạn xây dựng các ứng dụng Website có sử dụng MySQL. Điển hình như:

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.
  • Hiển thị bài ngẫu nhiên bên Sidebar
  • Hiển thị bài ngẫu nhiên liên quan đến bài viết

1. Lấy record ngẫu nhiên bằng ORDER BY RAND()

MySQL không tích hợp bất kỳ câu lệnh nào để chọn các record ngẫu nhiên từ một bảng. Để thực hiện điều này ta bạn sử dụng hàm RAND ().

Truy vấn sau đây chọn một hàng ngẫu nhiên trong một table bất kì:

SELECT * FROM table_name
ORDER BY RAND()
LIMIT 1;

Giải thích:

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

  • Hàm RAND() sẽ sinh ra giá trị ngẫu nhiên cho từng record trả về.
  • Lệnh ORDER BY sẽ sắp xếp các records theo giá trị mà hàn RAND sinh ra
  • LIMIT là giới hạn số record mà bạn sẽ lấy.

Nếu bạn muốn chọn N bản ghi ngẫu nhiên từ bảng cơ sở dữ liệu, bạn cần thay đổi mệnh đề LIMIT như sau:

SELECT * FROM table_name
ORDER BY RAND()
LIMIT N;

Giả sử ta có table như sau:

customers png

Và câu SQL dưới đây sẽ lấy record ngẫu nhiên từ bảng này.

SELECT 
    customerNumber, 
    customerName
FROM
    customers
ORDER BY RAND()
LIMIT 5;

Lưu ý rằng khi bạn chạy lệnh này thì mỗi lần chạy kêt quả sẽ khác nhau, bởi giá trị mà hàm RAND sinh ra là ngẫu nhiên.

Kỹ thuật này hoạt động rất tốt với table nhỏ. Tuy nhiên, nó sẽ chậm đối với table lớn vì MySQL phải sắp xếp toàn bộ dữ liệu để chọn ngẫu nhiên.

Tốc độ của truy vấn cũng phụ thuộc vào số hàng trong bảng. Bảng càng có nhiều hàng, thì càng mất nhiều thời gian để tạo số ngẫu nhiên cho mỗi hàng.

  • => Nói chung là không tối ưu, không nên sử dụng

2. Lấy record ngẫu nhiên bằng lệnh JOIN

Kỹ thuật này yêu cầu bảng phải có khóa chính tăng tự động (auto increment) và không có khoảng trống trong dãy thứ tự tăng dần. Tức là tăng dần 1, 2 ,3 thì ok, còn bị khuyết một giá trị như 1, 2, 5 ,6 thì không được.

Truy vấn sau tạo một số ngẫu nhiên dựa trên cột khóa chính:

SELECT 
   ROUND(RAND() * ( SELECT MAX(id) FROM  table_name)) AS id;

Chúng ta có thể JOIN table với kết quả này để lấy record ngẫu nhiên.

SELECT t.*
FROM table_name AS t
INNER JOIN
    (SELECT ROUND(
       RAND() * 
      (SELECT MAX(id) FROM table_NAME )) AS id
     ) AS x
WHERE
    t.id >= x.id
LIMIT 1;

Sử dụng kỹ thuật này bạn phải thực hiện truy vấn nhiều lần nếu muốn lấy nhiều record ngẫu nhiên, bởi vì câu lệnh sub query chỉ trả về một giá trị random mà thôi.

Truy vấn sau đây trả về một khách hàng ngẫu nhiên từ bảng customer.

SELECT 
    t.customerNumber, t.customerName
FROM
    customers AS t
        JOIN
    (SELECT 
        ROUND(RAND() * (SELECT 
                    MAX(customerNumber)
                FROM
                    customers)) AS customerNumber
    ) AS x
WHERE
    t.customerNumber >= x.customerNumber
LIMIT 1;

3. Lấy record ngẫu nhiên bằng JOIN và tạo biến

Trong trường hợp bảng có cột khóa chính id với các giá trị nằm trong phạm vi 1..N, bạn có thể sử dụng kỹ thuật sau:

  • Đầu tiên, chọn các số ngẫu nhiên trong phạm vi 1..N.
  • Thứ hai, chọn các bản ghi dựa trên các số ngẫu nhiên.
  • Câu lệnh sau giúp bạn thực hiện điều này:
SELECT 
    table. *
FROM
    (SELECT 
        ROUND(RAND() * (SELECT 
                    MAX(id)
                FROM
                    table)) random_num,
            @num:=@num + 1
    FROM
        (SELECT @num:=0) AS a, table
    LIMIT N) AS b,
    table AS t
WHERE
    b.random_num = t.id;

Trên là ba cách đơn giản nhất để lấy record ngẫu nhiên trong MySQL. Bạn có thể áp dụng để đưa vào dự án của mình.

Trong ba cách đó thì bạn không nên sử dụng cách 1 nhé, nó sẽ không tốt nếu dữ liệu quá lớn.

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