Home Database Mysql Tutorial mysql实现随机查询

mysql实现随机查询

Jun 07, 2016 pm 03:09 PM
linux mysql accomplish Inquire Community Enter random

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 一、随机查询一条数据 方法一: SELECT * FROM `table` ORDER BY RAND() limit 1 评价:不建议使用,效率非常低,官方文档中进行说明:Order By和RAND()连用,会多次扫描表,导致速度变慢。 方法

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

  一、随机查询一条数据

  方法一:

  SELECT * FROM `table` ORDER BY RAND() limit 1

  评价:不建议使用,效率非常低,官方文档中进行说明:Order By和RAND()连用,会多次扫描表,导致速度变慢。

  方法二:

  SELECT * FROM `table`

  WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `table`)))

  ORDER BY id LIMIT 1;

  解释:SELECT MAX(id) FROM `table` 这句话查询出最大的id值

  SELECT floor(RAND() * (SELECT MAX(id) FROM `table`))

  这句获取一个小于MAX(id)的随机数

  WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `table`)))

  这句话筛选出所有的大于生成随机数的id的行

  然后最后就把大于这个随机id的行查询出来,然后按照id排序,选择第一个,就相当与获取了所有行中随机的一行。

  评价:有问题,如果id不是从0开始的话,比如从10000开始自增,那么 SELECT floor(RAND() * (SELECT MAX(id) FROM `table`))

  得到的将是会哟很大概率得到小于10000的值,经过where限定的查询结果将会是所有的查询结果的几率变大,最后limit 1获取的是第一行数据的几率变高。

  方法三:

  SELECT * FROM `table`

  WHERE id >= (SELECT floor( RAND() *

  ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`)) + (SELECTMIN(id) FROM `table`)))

  ORDER BY id LIMIT 1;

  方法四:

  SELECT *

  FROM `table` AS t1 JOIN (SELECT ROUND(RAND() *

  ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`))+ (SELECT MIN(id) FROM `table`))

  AS id)

  AS t2

  WHERE t1.id >= t2.id

  ORDER BY t1.id LIMIT 1;

  评价:解决了方法二中MAX(id)的问题,RAND() * ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`)) + (SELECTMIN(id) FROM `table`)可以获取MAX(id)和MIN(id)中的随机数。

  以上解决方案都默认有一个不重复的数字字段,其实现在很多表的设计都是以一个自增段作为主键,当然还有一些是以uuid作为主键的,而没有数字键,这样的话,可以用mysql的函数将uuid的字符串转换成数字。而且还有一个问题,如果id字段的数字分布不均匀的话(比如按照1,4,5,6,7,8,45这样分布),也会造成随机查询的不合理,但是这里就不讨论那么复杂的问题了。

  二、随机查询多条数据

  方法一:把随机查询一条数据的limit

  1修改成limit 5

  评价:这样获取的数据会是连续的。

  方法二:

  SELECT *

  FROM `table` AS t1 JOIN ( SELECT ROUND(RAND() *

  ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`))+(SELECT MIN(id) FROM `table`))

  AS id  from `table` limit 50)

  AS t2 on t1.id=t2.id ORDER BY t1.id LIMIT 1;

  解释:

  SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`))+(SELECT MIN(id) FROM `table`)) AS id

  from `table` limit 50)这样会获取50个随机数字,然后on

  t1.id=t2.id会挑选出不大于50行的随机数据,然后取5条就好了。

mysql实现随机查询

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

How to switch Chinese mode with vscode How to switch Chinese mode with vscode Apr 15, 2025 pm 11:39 PM

VS Code To switch Chinese mode: Open the settings interface (Windows/Linux: Ctrl, macOS: Cmd,) Search for "Editor: Language" settings Select "Chinese" in the drop-down menu Save settings and restart VS Code

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

vscode setting Chinese tutorial vscode setting Chinese tutorial Apr 15, 2025 pm 11:45 PM

VS Code supports Chinese settings, which can be completed by following the steps: Open the settings panel and search for "locale". Set "locale.language" to "zh-CN" (Simplified Chinese) or "zh-TW" (Traditional Chinese). Save settings and restart VS Code. The settings menu, toolbar, code prompts, and documents will be displayed in Chinese. Other language settings can also be customized, such as file tag format, entry description, and diagnostic process language.

How to execute code with vscode How to execute code with vscode Apr 15, 2025 pm 09:51 PM

Executing code in VS Code only takes six steps: 1. Open the project; 2. Create and write the code file; 3. Open the terminal; 4. Navigate to the project directory; 5. Execute the code with the appropriate commands; 6. View the output.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

See all articles