Guy I'm trying to concat not null value from a list column.
i don't need that null in value
SELECT Emp_fname, Call_number, concat( Aud_name,Mag_name,Boo_name) as name
FROM manage left JOIN call_number ON manage.Man_Call_id = call_number.Call_id
left JOIN book_ ON call_number.Call_id = book_.Boo_id
left JOIN employee_ ON manage.Man_emp_id = employee_.Emp_id
left JOIN audiovisual_ ON call_number.Call_id = audiovisual_.Aud_Call_id
left join magzine_ ON call_number.Call_id = magzine_.Mag_Call_id
not concat
concat
The CONCAT function, if passed even a single NULL value, will just return NULL as a result. If you want to ignore possible NULL values when calling CONCAT with a list of columns, then you may use COALESCE. Here is one version of your query which would completely ignore NULL:
SELECT
Emp_fname,
Call_number,
CONCAT(COALESCE(Aud_name, ''), COALESCE(Mag_name, ''), COALESCE(Boo_name, '')) AS name
FROM manage
...
If you don't want to replace the nulls with empty string, you may use any string replacement you wish.
Related
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.
I have the following stored procedure
BEGIN
SELECT kids.*, SUM(point) as `point_sum`
FROM kids
LEFT JOIN tasks
ON kids.id = tasks.kid_id
WHERE kids.user_id = IN_user_id
GROUP BY kids.name;
END
This statement works fine.
My Question: the SUM(point) for new users are typically NULL because there is no submitted value yet to be summed.
What I want is if SUM(point) is NULL then it should return value like 0 but otherwise it should present the sum. I have looked around and not sure how to fix it, any good ideas?
You could use the coalesce function:
SELECT kids.*, COALESCE(SUM(point), 0) as `point_sum`
FROM kids
LEFT JOIN tasks
ON kids.id = tasks.kid_id
WHERE kids.user_id = IN_user_id
GROUP BY kids.name;
All you really need is IFNULL():
SELECT kids.*, IFNULL(SUM(point), 0) AS point_sum
That converts NULL to the supplied value, in this case 0.
I must be doing something wrong, or I don't understand the "IS NOT NULL" part, should it be showing me rows with NULL columns that I specifically wanted to no be NULL?
You are doing a left outer join on guesses. When the condition in the on is false, no row will be returned for that join and the fields referenced from that table will be null.
You have a few options I think, all depending on your needs:
Put this condition in the where clause;
Don't use the left outer join, but just a regular join;
Use coalesce to default the values.
Try use the below constructed query
select * from Tbl_EmployeeDetails where (name is not NULL or name <> 'null')
Just user regular join instead of left outer join :
SELECT guesses.*, games.* FROM games,guesses
WHERE guesses.game_id = games.game_id
AND games.real_score_team_1 IS NOT NULL AND games.real_score_team_2 IS NOT NULL ;
That should do the trick
The following code below gives me a result of
but how do I make it so that I get a result of
Code:
SELECT * FROM `shop_shipping_rules` LEFT JOIN `countries` ON `shop_shipping_rules`.shop_shipping_rule_country_iso = `countries`.iso
LEFT JOIN `shop_shipping_regions` ON `shop_shipping_regions`.shop_shipping_rule_region_code = `shop_shipping_rules`.shop_shipping_rule_region_code
WHERE `website_id` = 64 AND `shop_shipping_rule_name` IS NOT NULL
In your predicate, filter out empty fields.
AND `shop_shipping_rule_name` IS NOT NULL AND `shop_shipping_rule_name` <> ''
It's also best practice to stay away from SELECT * and use a column list. Also, you should use aliases and be explicit with your columns.
You will probably want to go through these and make the empty values NULL:
UPDATE `shop_shipping_rules`
SET `shop_shipping_rule_name` = NULL
WHERE `shop_shipping_rule_name` = ''
The issue I am having is that the IF function in MySQL is not correctly telling me if an ID is or is not located in the second table. Here is where you can view what I am doing
http://sqlfiddle.com/#!2/501513/4'
SELECT c.id AS clientID, IF (e.id, 'yes', 'no') AS hasID
FROM Table1 c LEFT JOIN Table2 e ON (c.id = e.id)
WHERE c.id IN ("123456","H100512","94061","OW59556","OR37615");
If you notice that the values "H100512" and "w76789" should both say 'yes' and not 'no' because they are found in the second table. I notice that if I take away the letter from the id in the query and in the table then it will correctly say whether it is there present in the table or not. Am I doing something wrong in the IF Function?
As a matter of course, I think it is better to declare the ids as varchar() and to use single quotes rather than double quotes to delimit character strings. However, neither of these is the cause of your problem.
The problem is the statement:
if(e.id, 'yes', 'no')
In MySQL, this is checking:
if(e.id <> 0, 'yes', 'no')
You are probably thinking that it is checking for NULL. Nope. So, what is happening is that a string like 'H100512' is being converted to an integer -- and it gets converted to 0 which fails the test.
I think you should write the query as:
SELECT c.id AS clientID,
(case when e.id is not null then 'yes' else 'no' end) AS hasID
FROM Table1 c LEFT JOIN
Table2 e
ON c.id = e.id
WHERE c.id IN ('123456', 'H100512', '94061', 'OW59556', 'OR37615', 'w76789');
This is explicit in what it is doing and it uses the ANSI standard conditional statement.