MySql - select 随机 4 个数据 来自一列的 2 个不同的唯一值

MySql - select random 4 data 2 different unique values from a column

我正在尝试获取一个随机数据集,其中包含来自 t table 的具有特定条件的 4 个数据。

我尝试选择了 4 个符合以下条件的数据

  1. f4中必须有2个'Y'和2个'C'数据是从数据集ttable中随机选择的(可以是Y-Y-C-CC-Y-Y-CC-C-Y-Y 等)
  2. 那 4 个数据中必须只有一个数据集的唯一数据不同 f2

所以可以是A-C-F-HA-D-I-HJ-H-E-C

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

SQL Fiddle

MySQL 5.6 架构设置:

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');

查询 1:

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

Results:

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

我期望的是;

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

使用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 值。

暴力破解法:

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

此查询将 return 所有可能的行 ID 组合。在子查询中选择一个随机组合并再次将其与您的 table 连接以获得相应的行:

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