在一个数据库测验中,我有两个不同的表节和子节。section表有2列section\u id和section\u title,而subsection表有4列section\u id、subsection\u id(ai)、subsection\u title和subsection\u detail。现在,我想要的是节标题,但需要查询的表是子节表,在子节表的节id的帮助下,我们必须从节表中获取节标题。我试过这里给出的一些解决方案,但我不知道为什么这些对我不起作用。如有任何帮助,我们将不胜感激。
daolsyd01#
你可以用 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])
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])
vecaoik12#
这应该对你有用。它将从subsection表中选择节标题和所有内容。
SELECT section.section_title, subsection.* FROM section, subsection WHERE section.section_id = subsection.section_id
添加此项以返回特定节:
AND section.section_id = $id
2条答案
按热度按时间daolsyd01#
你可以用
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])
vecaoik12#
这应该对你有用。它将从subsection表中选择节标题和所有内容。
添加此项以返回特定节: