为MySQL中的每个列值选择三个具有固定字符数的随机记录
为此,您可以使用CHAR_LENGTH()。使用RAND()进行随机记录。让我们首先创建一个表-
create table DemoTable (Subject text);
示例
使用插入命令在表中插入一些记录-
insert into DemoTable values('C');insert into DemoTable values('MySQL');
insert into DemoTable values('Java');
insert into DemoTable values('MongoDB');
insert into DemoTable values('RubyOnRails');
insert into DemoTable values('C++');
insert into DemoTable values('Python');
使用select语句显示表中的所有记录-
select *from DemoTable;
输出结果
+-------------+| Subject |
+-------------+
| C |
| MySQL |
| Java |
| MongoDB |
| RubyOnRails |
| C++ |
| Python |
+-------------+
7 rows in set (0.00 sec)
以下是查询以选择3条随机记录,每个记录的字符数固定-
select *from(select *from DemoTable WHERE CHAR_LENGTH(Subject) = 7 ORDER BY RAND() LIMIT 1 ) tbl
UNION
(select *from DemoTable WHERE CHAR_LENGTH(Subject) = 11 ORDER BY RAND() LIMIT 1 )
UNION
(select *from DemoTable WHERE CHAR_LENGTH(Subject) = 5 ORDER BY RAND() LIMIT 1 );
输出结果
+-------------+| Subject |
+-------------+
| MongoDB |
| RubyOnRails |
| MySQL |
+-------------+
3 rows in set (0.04 sec
以上是 为MySQL中的每个列值选择三个具有固定字符数的随机记录 的全部内容, 来源链接: utcz.com/z/361368.html