我正在尝试编写一个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方面,因为它每次都会给出相同的结果。
答案 0 :(得分:1)
获取四个随机属性的最简单方法是每个城市使用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
)