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

时间:2022-03-01 14:15:35

I have 2 different tables SECTION and SUBSECTION in a database QUIZ. The SECTION table has 2 columns section_id and section_title, whereas SUBSECTION table has 4 columns section_id, subsection_id(AI), subsection_title, subsection_detail. Now, what I want is that the section_title but the table that needs to be queried is the SUBSECTION table and with the help of the section_id from the SUBSECTION table we would have to fetch the section_title from the SECTION table.

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

I have tried some of the solutions given over here but I don't know why these aren't working for me. Any help here would be highly appreciated.

我已经尝试了一些在这里给出的解决方案,但我不知道为什么这些不适合我。这里的任何帮助将受到高度赞赏。

2 个解决方案

#1


1  

You could use IN in a way that the results from one query can be passed to another.

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

Perhaps this is an idea:

也许这是一个想法:

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])

SELECT section_title FROM section WHERE section_id IN(SELECT section_id FROM subsection WHERE [这里有一些条件从subsection表中获取正确的id组])

#2


1  

This should work for you. It will select the section_title and everything from the subsection table.

这应该适合你。它将从子节表中选择section_title和所有内容。

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

Add this to return a specific section:

添加此项以返回特定部分:

AND section.section_id = $id

#1


1  

You could use IN in a way that the results from one query can be passed to another.

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

Perhaps this is an idea:

也许这是一个想法:

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])

SELECT section_title FROM section WHERE section_id IN(SELECT section_id FROM subsection WHERE [这里有一些条件从subsection表中获取正确的id组])

#2


1  

This should work for you. It will select the section_title and everything from the subsection table.

这应该适合你。它将从子节表中选择section_title和所有内容。

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

Add this to return a specific section:

添加此项以返回特定部分:

AND section.section_id = $id