MySql - 从列中选择随机4个数据2个不同的唯一值

时间:2018-01-06 20:44:23

标签: mysql sql database nested-queries

我正在尝试从t表中获取具有特定条件的4个数据的随机数据集。

我尝试选择条件低于

的4个数据
  1. f4必须有2个'Y'和2个'C'数据,这些数据是从数据集t表中随机选择的(可以是Y-Y-C-CC-Y-Y-C或{{1}等等)
  2. 该4个数据中必须只有一个数据集的唯一数据C-C-Y-Y
  3. 因此可以是f2A-C-F-HA-D-I-H等。

    到目前为止我做到了这一点,但我无法在4个数据上得到2'C'和2'Y'。

    SQL Fiddle

    MySQL 5.6架构设置

    J-H-E-C

    查询1

    create table t ( id int, f2 char, f3 char, f4 char );
    insert into t values
    (1  ,'a'   ,'q'   ,'C'),
    (2  ,'a'   ,'w'   ,'Y'),
    (3  ,'b'   ,'e'   ,'C'),
    (4  ,'b'   ,'r'   ,'Y'),
    (5  ,'c'   ,'t'   ,'C'),
    (6  ,'c'   ,'y'   ,'Y'),
    (7  ,'d'   ,'u'   ,'C'),
    (8  ,'d'   ,'o'   ,'Y'),
    (9  ,'e'   ,'m'   ,'C'),
    (10  ,'e'   ,'n'   ,'Y');
    

    Results

    select f2, f3, f4
    from (
     select f2, f3, f4
     from (
      select f2, f4, f3 from
       ( select f2, f4, f3
         from t
         order by rand()
       ) t0
      group by f2
     ) t1  
     order by RAND() 
    ) t2 order by rand()
     LIMIT 4
    

    我的期望是什么;

    | f2 | f3 | f4 |
    |----|----|----|
    |  b |  r |  Y |
    |  e |  n |  Y |
    |  d |  o |  Y |
    |  a |  w |  Y |
    

2 个答案:

答案 0 :(得分:0)

使用UNION获取两个Y和两个C

SELECT * FROM (
    SELECT f2, f3, f4
    FROM t
    WHERE f4 = 'Y'
    ORDER BY RAND()
    LIMIT 2) AS y
UNION ALL
SELECT * FROM(
    SELECT f2, f3, f4
    FROM t
    WHERE f4 = 'C'
    ORDER BY RAND()
    LIMIT 2) AS c

但我不确定如何防止这两个子查询之间出现重复的f2值。

答案 1 :(得分:0)

蛮力方法:

select t1.id as id1, t2.id as id2, t3.id as id3, t4.id as id4
from t t1
join t t2 on t2.f2 not in (t1.f2)
join t t3 on t3.f2 not in (t1.f2, t2.f2)
join t t4 on t4.f2 not in (t1.f2, t2.f2, t3.f2)
where t1.f4 = 'C'
  and t2.f4 = 'C'
  and t3.f4 = 'Y'
  and t4.f4 = 'Y'

演示:http://rextester.com/VNF93190

此查询将返回所有可能的行ID组合。在子查询中选择一个随机组合,然后再次将其与表格连接以获取相应的行:

select t.*
from (
    select t1.id as id1, t2.id as id2, t3.id as id3, t4.id as id4
    from t t1
    join t t2 on t2.f2 not in (t1.f2)
    join t t3 on t3.f2 not in (t1.f2, t2.f2)
    join t t4 on t4.f2 not in (t1.f2, t2.f2, t3.f2)
    where t1.f4 = 'C'
      and t2.f4 = 'C'
      and t3.f4 = 'Y'
      and t4.f4 = 'Y'    
    order by rand()
    limit 1
) x
join t on t.id in (x.id1, x.id2, x.id3, x.id4)

演示:http://rextester.com/GQCCO60910