简体   繁体   English

选择一个随机的不同行

[英]Select a random distinct row

I have a table in mysql called drugs with 3 columns (id,illness,drug). 我在mysql中有一个名为药物的表,有3列(身份证,疾病,药物)。
An illness may occur several times in the column but with different drug . 可能会在色谱柱中多次出现疾病 ,但药物不同。

I need a code to select a distinct ilness but it should use a random criteria so that it can display different drug . 我需要一个代码来选择一个不同的ilness但它应该使用随机标准,以便它可以显示不同的药物

Example in mydb... mydb中的示例...

id | illness | drug
------------------------------
1  |malaria  |panadol    
2  |malaria  |hedex  
3  |malaria  |tripple action    
4  |fever    |panadol  

i tried this but it wasn't random in picking the rows for drug. 我试过这个但是在选择药物行时并不是随机的。 selected only a single drug for malaria all the time. 一直只选择一种治疗疟疾的药物。

$quotes="SELECT drug,remedy FROM drugs group by drug";

i need this outputs 我需要这个输出

 1   | malaria   | panadol or hedex or tripple action **//any but random**  
 2   | fever     | panadol **//random drug if i add other rows of fever**

尝试这个

$quotes="SELECT drug,remedy FROM drugs WHERE id = (1+ RAND()*(SELECT MAX(id) FROM drugs)) group by drug";

Or in mysql $select = 'select * from drugs order by rand() limit 1'; 或者在mysql $select = 'select * from drugs order by rand() limit 1'; That will get the rows at random, and then limit answer just for one of it. 这将随机获取行,然后限制其中一个行的答案。 You can add where clause to the query as well. 您也可以在查询中添加where子句。

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

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