Find all Values greater than X and replace them with 0 [closed] - mysql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hey guys I got a problem I don't remember the command to select all values in a column which are greater than X for replacing them with 0... Would be nice if someone could help me

You're looking for UPDATE statement
UPDATE table_name
SET column_name = 0
WHERE column_name > X
Here is SQLFiddle demo

update myTable set myField = 0 where myField > x

try this
update tableX set colY = 0 where colY > 23;
Of course 23 can be parameterized as X if you want.

Related

Order by expression [closed]

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
How would I do the following:
select * from table order by istrue(field > 90)
What does istrue() do? I don't recognize it as a MySQL function.
Perhaps your intention is to order things so the values of field greater than 90 are first. If so:
order by (field > 90) desc
In a numeric context, MySQL treats boolean expressions as integers with "1" being true. Hence the desc to get the true values first.
use Case statement;
select * from table
order by
case when field > 90 then ordercolumn end
Maybe what your trying to achieve is this:
SELECT * FROM table WHERE field > 90 ORDER BY field
I think that this is what you want:
SELECT * FROM table ORDER BY (field > 90) DESC
Or maybe:
SELECT * FROM table WHERE field > 90 ORDER BY field DESC

replacing column values with Y and N [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have a table which contains column Flag with values(Y,N) ,when I write select Query it should return me result as (Yes,No) .how to achieve that????Please Help
Try this:
SELECT CASE Flag
WHEN 'Y' THEN 'Yes'
ELSE 'No'
END AS Flag
FROM TableName
Syntax:
CASE column_name
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE result
END
Read more about CASE here.
Try this
SELECT CASE Flag
WHEN 'Y' THEN 'Yes'
ELSE 'No'
END AS Flag
FROM TABLENAME
You can also try if condition apart from casing as suggested above:
SELECT IF(flag='Y','Yes','No') FROM mytable;

MySQL if statements (with method invocations)? [closed]

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.

MySQl - Not able to get the NULL values from "ans_desc" column according to "ques_id" column [closed]

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

Explanation of nullsafe [closed]

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'