我正在开展一个彩票游戏项目,所以我们必须做一个随机的预选。
这是表格结构:
-------------------------------------------
|id| gamer_name | gender | city | job |
-------------------------------------------
|1 | name1 | Male | city1| job 1 |
-------------------------------------------
|2 | name2 | female | city2| job2 |
-------------------------------------------
我必须在表上写一个查询,随机获得200行,条件如下:
哪些查询适合这些结果?
答案 0 :(得分:0)
这是一个开始的基础。
如果要选择随机数量的项目,请使用
行SELECT column FROM table
ORDER BY RAND()
LIMIT 10
因此,如果你想让一半的人成为男性,那么将其限制为100
但是,我不知道你的百分比是如何加起来的...你试图选择160%?
答案 1 :(得分:0)
仅使用Mysql。
Create table Male like YOUR_TABLE;
INSERT INTO Male SELECT * from (SELECT * FROM YOUR_TABLE WHERE gender="Male");
Create table Result like YOUR_TABLE;
INSERT INTO Result (SELECT * from Male where city not in ('city1', 'city2') ORDER BY RAND() LIMIT 40);
INSERT INTO Result (SELECT * from Male where city='city1' and id not in (SELECT id from Result) ORDER BY RAND() LIMIT 10);
INSERT INTO Result (SELECT * from Male where city='city2' and id not in (SELECT id from Result) ORDER BY RAND() LIMIT 50);
您可能需要稍微调整一下查询。