我正在尝试从t表中获取具有特定条件的4个数据的随机数据集。
我尝试选择条件低于
的4个数据f4
必须有2个'Y'和2个'C'数据,这些数据是从数据集t表中随机选择的(可以是Y-Y-C-C
或C-Y-Y-C
或{{1}等等)C-C-Y-Y
。因此可以是f2
或A-C-F-H
或A-D-I-H
等。
到目前为止我做到了这一点,但我无法在4个数据上得到2'C'和2'Y'。
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 |
答案 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)