Search code examples
phpsqlrecords

How to fetch one of each record from table with an SQL query?


I have table containing information about a series of x number of quizzes. It includes a field for ID, Name, Subject and Level.

I would like to be able to fetch all the subjects once, so not the duplicates, because some quizzes will have the same subject.

I am then going to populate a drop down menu with this result and allow user to use it to filter their search results - if I can!

What I am stuck on is the SQL query, and I would be so grateful for any suggestions, thanks in advance!


Solution

  • Assuming Subject is a text field containing something like Math or English.

    select distinct subject from quizzes 
    

    Or for db's that lake distinct

    select subject from quizzes group by subject
    

    It is 'proper' however if you normalize subject/level into its own table since that information is probably repeated over and over for each quiz.