gpt4 book ai didi

mysql - 从 2 个 Mysql 表中为每个问题选择 4 个答案

转载 作者:行者123 更新时间:2023-11-29 07:53:11 25 4
gpt4 key购买 nike

我有两个表“问题”和“答案”,我需要选择 4 个随机问题及其 4 个答案

我尝试了这个:(但每个问题仅返回 1 个答案)

SELECT question.id,question.text,answer.id,answer.text
FROM question
LEFT JOIN answer ON answer.question_id = question.id
ORDER BY RAND( )
limit 4

表格:

问题

id     text
1 where is...?
2 is .....?
3 How old ...?
4 from where...?
5 where are ...?
6 is England ...?

回答

id   question_id     text
1 1 bla..
2 1 18
3 1 19
4 1 jj
5 2 kk
6 2 82
7 2 77
8 2 77
9 3 6
10 3 2

谢谢

最佳答案

首先使用子查询选择随机问题,然后加入答案表:

SELECT q.id,
q.text,
a.id AS a_id -- always use an alias when you have duplicate coloumn names
a.text a_text
FROM
(SELECT
id, text
FROM
questions
ORDER BY RAND() LIMIT 4) q
JOIN
answers a
ON a.question_id = q.id

编辑:键入时发布答案

关于mysql - 从 2 个 Mysql 表中为每个问题选择 4 个答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25884436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com