我正在尝试编写一个 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/