This question already has answers here:
MySql Query Replace NULL with Empty String in Select
(10 answers)
Closed 4 months ago.
I am looking to replace the NULL values that occur as a result of a SQL JOIN statement, with 'N/A'.
I have tried to set the default value of both related columns from both tables to N/A, however, every time I execute the SQL JOIN statement, I still receive NULL values.
The two tables I have are the clients and Medical_Aid tables, which I have connected using a foreign key called Reg_No. Below is my sql join query
SELECT
clients.Id_Number,
clients.Medical_No,
medical_aid.Name AS Medical_Aid,
clients.First_Name,
clients.Last_Name,
clients.Age,
clients.Gender,
clients.Email,
clients.Telephone
FROM
clients
LEFT OUTER JOIN medical_aid ON clients.Reg_No = medical_aid.Reg_No;
I have tried to set the default value of the Medical_No and Medical_Name as 'N/A' but every time I execute a JOIN statement, NULL values are returned on the Medical_Name column only
Therefore, I am expecting the JOIN Statement to return 'N/A' for both the Medical_No and medical_AidName
SELECT
clients.Id_Number,
ISNULL(clients.Medical_No,'N/A'),
ISNULL(medical_aid.Name, 'N/A') AS Medical_Aid,
clients.First_Name,
clients.Last_Name,
clients.Age,
clients.Gender,
clients.Email,
clients.Telephone
FROM
clients
LEFT OUTER JOIN medical_aid ON clients.Reg_No = medical_aid.Reg_No;
For the values from the medial_aid table you can use the IsNull() function to replace a NULL with a different value:
IsNull(medical_aid.Name, 'N/A') AS Medical_Aid
If you want to also replace another field from the clients table when no record is found in the medical_aid table, you may need to use a CASE statement:
CASE WHEN medical_aid.Reg_No is null THEN 'N/A' else clients.Medical_No END AS Medical_No
This statement says that when medical_aid.Reg_No is NULL (since there was no record from the medical_aid found to join to the clients table) then output 'N/A', otherwise output clients.Medical_No.
This question already has answers here:
MySQL Conditional Insert
(13 answers)
Closed 8 years ago.
A complex mysql question! I only want to insert the last value (with the zero values) if there is no other row with value 1420070400, but i cant put an index on the row (so i can use on duplicate key). Is there a way to do this on an other way?
INSERT INTO data_prijzen_advertentie (
`ID_advertentie`,`jaar`,`rijnr`,`status_prijs`,`datum_dag`,`timestamp_dag`,
`prijs_maand`,`prijs_week`,`prijs_midweek`,`prijs_langweekend`,`prijs_weekend`,
`prijs_dag`,`prijs_ochtend`,`prijs_middag`
)
VALUES
(100,2014,1,1,'12-05-2014',1399852800,0,100,0,75,0,0,0,0),
(100,2014,2,1,'23-05-2014',1400803200,0,75,0,101,0,0,0,0),
(100,2014,3,1,'30-05-2014',1401408000,0,100,0,75,0,0,0,0),
(100,2014,4,1,'01-01-2015',1420070400,0,0,0,0,0,0,0,0)
ON DUPLICATE KEY UPDATE
status_prijs = VALUES(status_prijs), datum_dag = VALUES(datum_dag),
timestamp_dag = VALUES(timestamp_dag), prijs_maand = VALUES(prijs_maand),
prijs_week = VALUES(prijs_week), prijs_midweek = VALUES(prijs_midweek),
prijs_langweekend = VALUES(prijs_langweekend), prijs_weekend = VALUES(prijs_weekend),
prijs_dag = VALUES(prijs_dag), prijs_ochtend = VALUES(prijs_ochtend),
prijs_middag = VALUES(prijs_middag);
One way to do this is to use a SELECT in place of the VALUES clause. Use the SELECT statement to return the rows you want inserted. For example:
SELECT 100 AS a,2014 AS b,1 AS c,1 AS d,'12-05-2014' AS e
,1399852800 AS timestamp_dag
,0 AS g,100 AS h,0 AS i,75 AS j,0 AS k,0 AS l,0 AS m,0 AS n
UNION ALL
SELECT 100,2014,2,1,'23-05-2014',1400803200,0,75,0,101,0,0,0,0
UNION ALL
SELECT 100,2014,3,1,'30-05-2014',1401408000,0,100,0,75,0,0,0,0
UNION ALL
SELECT 100,2014,4,1,'01-01-2015',1420070400,0,0,0,0,0,0,0,0
With a SELECT, it's also possible to check for rows that already exist in the table. You can use that query above as an inline view (i.e. wrap that query in parens and assign an alias for the derived table) and write a SELECT against it (like it was a table), and use a NOT EXISTS predicate or an anti-join pattern to suppress the return of rows where a "matching" row already exists in the target table. For example:
SELECT s.*
FROM ( SELECT 100 AS a,2014 AS b,1 AS c,1 AS d,'12-05-2014' AS e
,1399852800 AS timestamp_dag
,0 AS g,100 AS h,0 AS i,75 AS j,0 AS k,0 AS l,0 AS m,0 AS n
UNION ALL
SELECT 100,2014,2,1,'23-05-2014',1400803200,0,75,0,101,0,0,0,0
UNION ALL
SELECT 100,2014,3,1,'30-05-2014',1401408000,0,100,0,75,0,0,0,0
UNION ALL
SELECT 100,2014,4,1,'01-01-2015',1420070400,0,0,0,0,0,0,0,0
) s
LEFT
JOIN data_prijzen_advertentie t
ON t.timestamp_dag = s.timestamp_dag
WHERE t.timestamp_dag IS NULL
(NOTE: the LEFT JOIN operation returns all rows from the derived table s, along with matching rows from t; the "trick" is to use a WHERE clause that eliminates all rows that found a match, so we are left with rows from s that didn't have a matching row in t. The same thing could be achieved with a NOT EXISTS predicate with a correlated subquery.)
It may be necessary to wrap this query in parens and reference it as an inline view (so it is again, a derived table) to avoid a mutating table issue/error. For example:
SELECT r.*
FROM (
SELECT s.*
FROM (
SELECT 100 AS a,2014 AS b,1 AS c,1 AS d,'12-05-2014' AS e
,1399852800 AS timestamp_dag
,0 AS g,100 AS h,0 AS i,75 AS j,0 AS k,0 AS l,0 AS m,0 AS n
UNION ALL
SELECT 100,2014,2,1,'23-05-2014',1400803200,0,75,0,101,0,0,0,0
UNION ALL
SELECT 100,2014,3,1,'30-05-2014',1401408000,0,100,0,75,0,0,0,0
UNION ALL
SELECT 100,2014,4,1,'01-01-2015',1420070400,0,0,0,0,0,0,0,0
) s
LEFT
JOIN data_prijzen_advertentie t
ON t.timestamp_dag = s.timestamp_dag
WHERE t.timestamp_dag IS NULL
) r
Once you have a query working that returns the rows you want (excluding rows where a matching row already exists in the target table), you can substitute the VALUES clause in the INSERT statement with the query.
Note: this only checks for existence of rows already in the table when the query runs. This doesn't check the resultset returned by the query, to see if there are two (or more) rows with the timestamp_dag value.