如何从多个条件的mysql数据库中随机抽取样本

时间:2013-07-11 11:25:42

标签: mysql

我有一张表,其中包含一些用户信息,性别,出生日期,“类别”等

我需要随机抽样,但结果如下:

75%女性<25%男性
  

50%年龄在25-35岁之间   40%年龄在36-40岁之间   10%年龄在41-60之间

总计2000条记录。

无论如何直接在MySQL中执行此操作?

1 个答案:

答案 0 :(得分:1)

对于每个sql查询,您不能在Union Statement中使用order by或limit。它们可以在最后使用。

因此最好使用存储过程进行输出。

CREATE PROCEDURE USER_DATA_RAND()
BEGIN
CREATE TEMPORARY TABLE USER_TMP
ID INT,
DATE_OF_BIRTH DATE,
CATEGORY STRING,
AGE INT)
INSERT INTO USER_TMP
select sex, date_of_birth, `category`,age from users where age between 25 and 35 and sex = 'male' order by rand() limit 2000*0.5*0.25) ;
INSERT INTO USER_TMP
select sex, date_of_birth, `category`,age from users where age between 25 and 35 and sex = 'female' order by rand() limit 2000*0.5*0.75) ;
INSERT INTO USER_TMP
select sex, date_of_birth, `category`,age from users where age between 36 and 40 and sex = 'male' order by rand() limit 2000*0.40*0.25) ;
INSERT INTO USER_TMP
select sex, date_of_birth, `category`,age from users where age between 36 and 40 and sex = 'female' order by rand() limit 2000*0.40*0.75) ;
INSERT INTO USER_TMP
select sex, date_of_birth, `category`,age from users where age between 41 and 60 and sex = 'male' order by rand() limit 2000*0.10*0.25) ;
INSERT INTO USER_TMP
select sex, date_of_birth, `category`,age from users where age between 41 and 60 and sex = 'female' order by rand() limit 2000*0.10*0.75) ;

SELECT * FROM USER_TMP;
END