简体   繁体   English

MySQL选择9个随机和1个定义的记录

[英]MySQL select 9 random and 1 defined record

My question is pretty straightforward, how do I select 9 random records and one defined record in the same SQL statement? 我的问题很简单,如何在同一条SQL语句中选择9条随机记录和1条定义的记录? All I can think of so far is this, which only returns the one partner record (the defined one) but not the other 9 random ones. 到目前为止,我只能想到的是,它仅返回一个伙伴记录(已定义的记录),而不返回其他9个随机记录。

$sql2 = "SELECT * FROM table WHERE filename = '$partn' AND filename != '$x' ORDER BY RAND() LIMIT 10";

To elaborate, I need ten records to be returned, none of then should be '$x', one of them should be '$partn' and then 9 other random ones, they all need to be displayed in a random order. 详细地说,我需要返回十个记录,然后都不应该是“ $ x”,其中一个应该是“ $ partn”,然后是其他9个随机记录,它们都需要以随机顺序显示。

Any help would be appreciated!:) 任何帮助,将不胜感激!:)

use union: http://dev.mysql.com/doc/refman/5.0/en/union.html 使用联合: http : //dev.mysql.com/doc/refman/5.0/en/union.html

(select the one you want)
union
(select 9 randoms that arent the one you want)
(SELECT * FROM table WHERE file='$x')
UNION ALL
(SELECT * FROM table WHERE file<>'$x' ORDER BY RAND() LIMIT 9)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM