Skip to content
Advertisement

MySQL Filtering Results from a One-to-many Relationship

I have two tables. quotes_table and flags_table. The quotes_table has a one-to-many relationship with the flags_table – each quote can have multiple flags. flags_table.id_flag is a foreign key of quotes_table.id_flag.

Example Input

quotes_table

flags_table

Expected Output

In summary, I am trying to return all quotes that do not have a flag of “testing” even if other flags are present.

My current query is still returning quote_key record 111 because of the left join but I am not sure how to “filter” it out.

Current Query

Example Diagram of the quotes_tableand flags_table: Diagram showing relationship between quote and flag tables.

Result of performing a LEFT JOIN between the quotes_table and flags_table. LEFT JOIN Result between quotes and flags table

The rows in red represent a quote, notice the shared quote_key. So I am trying to throw away the entries in red from my results so that I only get quotes without a flag of “testing”.

SQL Fiddle Example

Advertisement

Answer

Hope this helps.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement