mysql - 从 REGEXP 查询返回随机行集

标签 mysql sql regex random

我正在尝试编写一个 MySQL 查询,该查询基于对一组城市的 REGEXP 搜索返回一组随机行。

即...

SELECT * FROM master_rets_table 
          WHERE property_type = "Residential" and city REGEXP "(JUNO BEACH|palm beach gardens|jupiter|WEST PALM BEACH)" and listing_price > 200000 and listing_price < 500000
          GROUP BY city
          ORDER BY RAND()

好消息是这确实会为每个城市返回一个属性,但事实证明,我猜 GROUP BY 消除了查询的 RANDom 方面,因为它每次都给出相同的结果。

最佳答案

获得四个随机属性(每个城市一个)的最简单方法是使用union all:

(select *
 from master_rets_table
 where property_type = "Residential" and city = 'JUNO BEACH' and listing_price > 200000 and listing_price < 500000
 order by rand()
 limit 1
) union all
(select *
 from master_rets_table
 where property_type = "Residential" and city = 'palm beach gardens' and listing_price > 200000 and   listing_price < 500000
 order by rand()
 limit 1
) union all
(select *
 from master_rets_table
 where property_type = "Residential" and city = 'jupiter' and listing_price > 200000 and listing_price < 500000
 order by rand()
 limit 1
) union all
(select *
 from master_rets_table
 where property_type = "Residential" and city = 'WEST PALM BEACH' and listing_price > 200000 and  listing_price < 500000
 order by rand()
 limit 1
)

关于mysql - 从 REGEXP 查询返回随机行集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27475625/

相关文章:

mysql - MYSQL SELECT 语句中如何划分子查询?

mysql - 总是在 SQL 中的最后一个条目之后将新数据插入表中

mysql - 为什么我的日期时间搜索不起作用?

c++ - 通过 ODBC API 从 SQL 查询中获取的数据大小

MySQL 函数将工作日数添加到 DATETIME

mysql - 重写查询以避免 not in 并提高性能

java - 为什么我的单词是一个空字符串,当我用\\s 拆分()我的字符串时

JavaScript 以编程方式创建正则表达式

regex - sed 非贪婪搜索的替代方案

mysql - 如何创建 BEFORE INSERT 触发器