Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have realized that these two queries do not work if 'fk_building' is null. I don't understand why, and cant seem to find a good explanation as it is kind of hard to google. Can someone explain why the hell !=3 doesnt return everything that is not 3??? including null rows? Why do I need to use <=> ?
update floor set fk_building = 3 where fk_building != 3 and floor_id = 1;
or
select * from floor where fk_building != 3
do not work where fk_building is null.
I would recommend looking at the "Common Mistakes" section on NULL values on Wikipedia.
From the entry:
For example, a WHERE clause or conditional statement might compare a column's value with a constant. It is often incorrectly assumed that a missing value would be "less than" or "not equal to" a constant if that field contains Null, but, in fact, such expressions return Unknown
As users have suggested, you can use a null safe operator if your RDBMS allows it, or check for IS NULL.
use the null safe operator
update floor set fk_building = 3 where (not fk_building <=> 3) and floor_id = 1;
http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html
Rather than get pissed off, why not just do:
update floor set fk_building = 3 where (fk_building != 3 OR fk_building IS NULL) and floor_id = 1;
You can also use x <=> 3 to test for both.
EDIT: in your case it be 'not x <=> 3'
Related
Hopefully this is a simple one but I am most likely over complicating this for myself. Purpose of this code is to find previous operation name within a specified list of operations that is still open and returning it. If it's closed to say 'CLOSED'.
So far I'm using subquery to get the correct operation name, as expected I'm getting some null results which indicates to me that operation is closed. I wanted to wrap my subquery into a case statement to say something along the lines of CASE WHEN it's null then 'CLOSED' else operation_name end.
(select work_center_no
from shop_order_operation
where order_no = so.order_no
and release_no = so.release_no
and sequence_no = so.sequence_no
and work_center_no in ('CNC','EXPF','LS3M','LS4M','LS6M','PLAS','SAW','TBPL','EXSAW')
and oper_status_code in ('Released','In Process') order by operation_no fetch first 1 row only )
Above is my subquery. Hope this makes sense.
Thanks!
K
You can nicely default a value using IFNULL, just change your select clause to this:
select IFNULL(work_center_no, 'CLOSED')
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
my requirement is to generate a mysql query as
select ss.column1 P1,ss.column2 P2,ss.column3 P3 from table1 ss;
i want to select column 2 field values only if it has a value in table1 else i want to insert a constant value into the P2 column. Can someone help me with framing a query for this.
COALESCE is the SQL function that let's you replace null values by something else. E.g.:
select
ss.column1 as p1,
coalesce(ss.column2, 'no value') as p2,
ss.column3 as p3
from table1 ss;
The data types must match however, so you can use the above when column2 is a text column. If it is numeric, you can replace null with a numeric value (e.g. with a zero) or you'd cast the columns' datatype.
Some examples:
coalesce(mytext, 'unknown')
coalesce(mynumber, 0)
coalesce(cast(mynumber as varchar), 'unknown')
coalesce(mydate, date(now()))
coalesce(date_format(mydate, '%Y-%m-%d'), 'unknown')
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do I do the following pseudocode in MySQL:
if (a IS NOT NULL)
{
GROUP BY (id)
num = COUNT(*)
} else {
num = 0
}
Note sure what you are trying to do .....
if you want to count non-null value, check those examples, it would look like this
COUNT(NULLIF(a, ''))
Also consider use Case if you need more complete counting
CASE
WHEN a = 'something' COUNT(1)
ELSE NULL
EDIT
If you want to invoke some query, depend on some value, consider use store procedure/ function
This may be the code you are looking for:
select num := count(distinct id)
from t;
This will set num to 0 if there are no rows.
If you really want the comparison to NULL:
select num := (case when a is not null then count(distinct id) else 0 end)
from t;
Note this assumes that a and num are not columns in the database.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
SELECT ques_id,ans_desc
FROM answer
ORDER BY ans_desc
HAVING ans_desc=0
Use the ans_desc IS NULL predicate in the WHERE clause:
SELECT ques_id, ans_desc
FROM answer
WHERE ans_desc = 0 OR ans_desc IS NULL
ORDER BY ans_desc ;
This will give you only those that have either 0 or NULL values in the ans_desc column.
The WHERE ans_desc = 0 alone won't give the NULL values.
I don't think you even need 'order by ans_desc' when you need to select only the rows with NULL values.
You should do something like:
SELECT ques_id as `question`, ans_desc as `answer`
FROM answer
WHERE ans_desc is NULL;
Advantage of this query is that it doesn't use filesort.
See output of explain for the query you are using to know more.
Hope that helps.
0 is different from NULL and different from empty
SELECT ques_id,ans_desc
FROM answer
WHERE ISNULL(ans_desc) OR ans_desc = ''
ORDER BY ans_desc
I need to correct this query! Thank!
I try:
UPDATE `item_proto_copy2`
SET `socket_pct`='1'
WHERE `vnum` END AS 7 AND `type`=1
do you mean?
UPDATE `item_proto_copy2`
SET `socket_pct`='1'
WHERE `vnum` = 7 AND `type`=1
if not, follow-up question, what do you want on this condition WHERE vnum END AS 7 AND type=1?
UPDATE 1
UPDATE `item_proto_copy2`
SET `socket_pct`='1'
WHERE RIGHT(`vnum`, 1) = '7' AND `type`=1
RIGHT
#JW's answer works by converting the vnum to a CHAR and then chopping it off with the RIGHT() function, keeping only the first character from the right:
WHERE RIGHT(vnum, 1) = '7'
You could also use LIKE in a similar way (implicit conversion to CHAR and then checking the right-most character):
WHERE vnum LIKE '%7'
And if the numbers are non-negative integers, this would work too, using modular arithmetic:
WHERE vnum MOD 10 = 7
Guessing at what you're trying to do in your where clause. The END does not belong in your WHERE clause and you cannot use AS in a WHERE clause either.
UPDATE `item_proto_copy2`
SET `socket_pct`='1'
WHERE `vnum` = 7 AND `type`=1