gpt4 book ai didi

php - 如何加入这些表?

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

我有三个表:

users
-----------------------------------
id | firstname | lastname | username
-----------------------------------
1 | John | Doe | jdoe
-----------------------------------
2 | Maria | T. | marty
-----------------------------------
3 | Alex | White | alexw
-----------------------------------

questions
--------------------------------------------
id | user_id | title
--------------------------------------------
1 | 2 | My first question?
--------------------------------------------
2 | 3 | One more question?
--------------------------------------------
3 | 3 | The third question?
--------------------------------------------

answers
----------------------------------------------
id | question_id | description
----------------------------------------------
1 | 2 | Answers to the 2nd question
----------------------------------------------
2 | 1 | Answer for 1st question
----------------------------------------------
3 | 1 | Another answer for 1st
----------------------------------------------

现在,我想检索包含用户(提问者)的名字、姓氏、用户名以及针对某个问题给出的总答案数的所有问题。

请帮忙写一下。由于我弄乱了我的查询,所以没有发布在这里。

谢谢

最佳答案

SELECT q.*, u.*, COUNT(a.id) as answer_count
FROM questions q
LEFT JOIN users u ON q.user_id = u.id
LEFT JOIN answers a ON a.question_id = q.id
GROUP BY q.id

关于php - 如何加入这些表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7860900/

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