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.
Related
I want to join below tables in such a way that that it should return matched value if not matching it should return a value as "NA".
below are the table details
available tables:
desired output:
Use Left Join between the tables, to consider those rows also which do not exist in the Process table.
You can use Ifnull() function, to set the value NA if no matching row (thus null value) in the Process table.
Try the following (change table and column name(s) accordingly):
SELECT r.Ticket_id,
r.Status,
r.Department,
r.Owner,
r.Process_id,
IFNULL(p.Proces_Name, 'NA')
FROM Resolution AS r
LEFT JOIN Process AS p ON p.Process_id = r.Process_id
If a value isn't found in a MySQL LEFT JOIN, the field is "filled" with NULL
The best practice, as I understand your question is IFNULL
SELECT
r.*,
IFNULL(process_name,'NA')
FROM
resolution r
LEFT JOIN processes p ON p.process_id = r.process_id
select a.ticket_id,a.status,a.department,a.ownername,
case
when b.process_name is null then 'NA'
else b.process_name
end as proces_name
from resolution_tab a
left join process_tab b
on a.process_id=b.process_id
order by a.ticket_id;
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.
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'm a mysql beginner these days, i appreciate for your advise.
I got a problem my sql query
INSERT IGNORE INTO TB_AUTO_BAN(MEMO, REG_DATE, USER_ID, NAME, PHONE_NUM)
(
SELECT 'test' AS MEMO, NOW() AS REG_DATE, a.USER_ID, a.NAME, a.CONTACT_NUM
FROM TB_CONTACT AS a,TB_CONTACT_GROUP AS b
WHERE b.USER_ID = 'spark#naver.com'
AND b.GROUP_CONTACT_SEQ = IN(12800,12801)
AND a.GROUP_CONTACT_SEQ = b.GROUP_CONTACT_SEQ
Bold text part is the problem, how should I modify it??
Your query looks ok, except for the = in. Only in is needed.
I would write the query as:
INSERT IGNORE INTO TB_AUTO_BAN(MEMO, REG_DATE, USER_ID, NAME, PHONE_NUM)
SELECT 'test' AS MEMO, NOW() AS REG_DATE, c.USER_ID, c.NAME, c.CONTACT_NUM
FROM TB_CONTACT AS c JOIN
TB_CONTACT_GROUP AS cg
ON c.GROUP_CONTACT_SEQ = cg.GROUP_CONTACT_SEQ
WHERE cg.USER_ID= 'spark#naver.com' AND
cg.GROUP_CONTACT_SEQ IN (12800, 12801) ;
In addition to fixing the condition, this changes the table aliases to be abbreviations rather than arbitrary letters. It also uses standard join syntax instead of implicit joins with the condition buried in the where clause.
the equality sign "=" is superfluous. just delete it, and leave: b.GROUP_CONTACT_SEQ IN (12800,12801)
I'm working with a little display complication here. I'm sure there's an IF/ELSE capability I'm just overlooking.
I have 2 tables I'm querying (customers, addresses). The first has the main record, but the second may or may not have a record to LEFT JOIN to.
I want to display a zero if there is no record in the addresses table.
And I want to only display 1, if a record exists.
What I've attempted so far:
SELECT c.name, COALESCE(a.addressid,0) AS addressexists
FROM customers c
LEFT JOIN addresses a ON c.customerid = a.customerid
WHERE customerid = 123
This first example does not do it. But I may be utilizing COALESCE wrong.
How can I display a 0, if null, and a 1, if something exists?
Instead of COALESCE(a.addressid,0) AS addressexists, use CASE:
CASE WHEN a.addressid IS NOT NULL
THEN 1
ELSE 0
END AS addressexists
or the simpler:
(a.addressid IS NOT NULL) AS addressexists
This works because TRUE is displayed as 1 in MySQL and FALSE as 0.
SELECT c.name, IF(a.addressid IS NULL,0,1) AS addressexists
FROM customers c
LEFT JOIN addresses a ON c.customerid = a.customerid
WHERE customerid = 123
Careful if you're coming from C/C++ and expecting this to work:
select if(name, 1, 0) ..
Even if 'name' is not NULL, unlike in C, a false-condition still triggers and the above statement returns 0. Thus, you have to remember to explicitly check for NULL or empty string:
select if(name is null or name = '', 0, 1)
PS Eugen's example up above is correct, but I wanted to clarify this nuance as it caught me by surprise.
SELECT
c.name,
CASE WHEN a.addressid IS NULL THEN 0 ELSE 1 END AS addressexists
FROM customers c
LEFT JOIN addresses a ON c.customerid = a.customerid
WHERE customerid = 123
Another method without WHERE, try this..
Will select both Empty and NULL values
SELECT ISNULL(NULLIF(fieldname,'')) FROM tablename
It will set null if it is an empty string, then be true on that also.
You can actually use an IF statement in the latest versions of MySQL.
IF(expr,if_true_expr,if_false_expr)
IE:
SELECT name, IF(ISNULL(name), 'robot', 'human') AS type
FROM visitors
If within TSQL, you can try :
SELECT IIF(a.addressid IS NULL, 0, 1) AS addressexists
SQL Server should work