Skip to content
Advertisement

Why doesn’t the WHERE work in this sql query

I’m writing a SQL query that needs to only show the results where the ‘titel’ is the same as ‘Baklava’, but it doesn’t return any rows. When I try to use the query without the WHERE, is shows all the results, but I need the WHERE clause to work.

Here is the query I’m trying to use:

import.sql:

Advertisement

Answer

You have three issues in your statement. The first is that your posts_tags table is empty, so an INNER JOIN produces an empty result set.

The second is an error in the INSERT statement that fills the tags table, so this is also empty.

And the third issue seems to be that the columns you join on do not seem to be logically correct.

Try adding the following to your import.sql:

and fixing the other INSERT statement:

The data now correctly indicates post 2 is related to tag 1.

Then fix your ON clauses so that they make sense:

This produces your desired result.

See this db<>fiddle

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