在PHP / MYSQL中从同一数据库的两个不同表中选择数据

时间:2018-09-04 08:42:54

标签: php mysql database web

我在数据库 QUIZ 中有2个不同的表 SECTION SUBSECTION SECTION 表具有2列 section_id section_title ,而 SUBSECTION 表具有4列 section_id subsection_id (AI), subsection_title subsection_detail 。现在,我想要的是 section_title ,但需要查询的表是 SUBSECTION 表,并借助 section_id SUBSECTION 表中,我们将必须从 SECTION 表中获取 section_title

我已经尝试过这里给出的一些解决方案,但是我不知道为什么这些解决方案对我不起作用。我们将不胜感激。

2 个答案:

答案 0 :(得分:1)

您可以使用IN来将一个查询的结果传递给另一个查询。

也许这是一个主意:

SELECT section_title FROM section WHERE section_id IN (SELECT section_id FROM subsection WHERE [some condition here to get the correct set of ids from subsection table])

答案 1 :(得分:1)

这应该为您工作。它将从小节表中选择section_title和所有内容。

SELECT section.section_title, subsection.* FROM section, subsection WHERE section.section_id = subsection.section_id

添加此代码以返回特定部分:

AND section.section_id = $id