I have a MySql database with 2 tables:
countries
results
id
and a country name
.country_id
, a value
and a date
.How can I create a query that lists all countries with their specific result, but still list them and shows 0 if there is no result on the results table?
There are two ways to replace NULL with blank values in SQL Server, function ISNULL(), and COALESCE(). Both functions replace the value you provide when the argument is NULL like ISNULL(column, '') will return empty String if the column value is NULL.
not equal to (<>, !=) operator. MySQL Not equal is used to return a set of rows (from a table) after making sure that two expressions placed on either side of the NOT EQUAL TO (<>) operator are not equal. Syntax: <>, != MySQL Version: 5.6.
The IS NULL operator is used to test for empty values (NULL values).
It returns either field1 if the row exists, otherwise 0.
Use a LEFT JOIN between the two tables
select c.id, c.name, IFNULL(r.value, 0) value, r.date
from countries c
LEFT JOIN results r on r.country_id = c.id
To show 0 (for the value
column) if there is no result, use IFNULL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With