Programming Geek
Programming Geek

Reputation: 139

Selecting data from two different tables of the same database in PHP/ MYSQL

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.

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.

Upvotes: 0

Views: 1049

Answers (2)

Mayari
Mayari

Reputation: 540

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

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

Upvotes: 1

Marjan
Marjan

Reputation: 201

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

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

Upvotes: 1

Related Questions