Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何将两个表中的sql结果集随机化?_Mysql_Sql_Random - Fatal编程技术网

Mysql 如何将两个表中的sql结果集随机化?

Mysql 如何将两个表中的sql结果集随机化?,mysql,sql,random,Mysql,Sql,Random,我有两张桌子 Table1 person: -person_id -name Table2 expertise: -expertise_id -person_id -expertise_name 我想要实现的是返回5个随机的人,并相应地返回该人的2个随机专业知识。到目前为止,我可以返回所有的人和他们的所有专业知识 SELECT * FROM person p, expertise e WHERE e.person_id = p.person_id 有人能帮我吗?提前谢谢。最简单的方法可能是

我有两张桌子

Table1 person:
-person_id
-name

Table2 expertise:
-expertise_id
-person_id
-expertise_name
我想要实现的是返回5个随机的人,并相应地返回该人的2个随机专业知识。到目前为止,我可以返回所有的人和他们的所有专业知识

SELECT * FROM person p, expertise e WHERE e.person_id = p.person_id

有人能帮我吗?提前谢谢。

最简单的方法可能是一行返回专家:

select p.*,
       substring_index(group_concat(e.expertise_name order by rand()), ',', 2) as two_expertises
from (select p.*
      from person p
      order by rand()
      limit 5
     ) p left join
     expertise e
     on p.person_id = e.person_id
group by p.person_id;