I want to use different conditions depending on a certain field value.
If for example the value of field1='abc' then field2 should be '123' else field3 should be '456'. My query is much more complex, but the question is whether I could use different conditinons depending on a field value.
Something like that:
SELECT * FROM table
WHERE
CASE table.field1='abc'
THEN table.field2='123'
ELSE table.field3='456'
I have a solution for my problem, but it goes with 3 subqueries which takes a lot of time to respond.
If for example the value of field1='abc' then field2 should be '123'
else field3 should be '456'
You don't really need a CASE statement here. Rather use different combination of condition like.
WHERE (field1 = 'abc' and field2 = '123')
OR (field1 != 'abc' and field3 = '456')
Related
I am trying to pull all rows where two fields don't match. I understand that nulls are essentially "unknowns" and so have taken steps to define nulls as 0's. As below.
Select ifnull(field1, 0), ifnull(field2, 0)
from table
where field1 != field2
Can anyone tell me why this doesn't work? I still get no results even though i feel i am defining nulls as "0" with ifnull(field1, 0) correctly.
It doesn't work because that is how NULL values work in relational databases. Almost all comparisons return NULL, which is treated as false.
Also, what you do in the SELECT has no impact on the WHERE. The fields are still coming from the original table.
More importantly is what you can do. The simplest method is to use a NULL-safe comparison:
where not field1 <=> field2
Because 0 is inserted after and not before you select
Should be instead:
Select * from
((Select *,ifnull(field1, 0) new_field1, ifnull(field2, 0) new_field2
from table) As query)
WHERE new_field1 != new_field2;
You should move the IFNULL() down to the WHERE clause like this:
Select ifnull(field1,0) as 'Field1', ifnull(field2, 0) as 'Field2'
from table
where ifnull(field1,0) != ifnull(field2,0)
Is it possible to use Select with Case to select from one of several fields depending on which of them is null or not?
I basically want to return a value for all records with logic that says
Return $Value from FIeldA if not null
else from FieldB if not NULL
else from FieldC if not Null
else '0'
I've used Case/When/Then to compare values from a specific field but not as a way of comparing values between fields and not sure if this is possible.
Short answer, use COALESCE:
SELECT COALESCE(FieldA, FieldB, FieldC, 0) AS FieldName
FROM tableName;
It will give you the first non nullable value from the three fields FieldA, FieldB, FieldC. If all are null, then it will return 0. Which what you are trying to do.
Long answer, use CASE expression.
if you want to check multiple filed and if null then return same result 0 then use COALESCE function . this is simple code
SELECT COALESCE(filed1, filed2, filed3, 0) as output from table;
for more information
https://www.w3schools.com/sql/func_sqlserver_coalesce.asp
extra option if you want to select filed using condition then use case. this is demo code
SELECT CASE 1 WHEN 1 THEN 'this is case one'
WHEN 2 THEN 'this is case two'
ELSE 'this is not in the case'
END as 'how to execute case statement';
for more information
https://dev.mysql.com/doc/refman/5.7/en/case.html
http://www.mysqltutorial.org/mysql-case-function/
suppose I do this:
SELECT * FROM table WHERE field2 = '2019#162440' OR field2 LIKE '%%2019#%%';
In this case, it will try to execute the matching of BOTH field2 = '2019#162440'and field2 LIKE '%%2019#%%' conditions (ie, it will search for rows matching those conditions hence it takes some more computation power to try to find rows matching both condition even if it already found a row matching field2 = '2019#162440')
Is there a way to instruct mysql by reforming the query to ONLY try to execute field2 LIKE '%%2019#%%' if the condition field2 = '2019#162440' does not match anything so that the query becomes more efficient
IE. I essentially want mysql to only try to find rows matching field2 LIKE '%%2019#%%' only if no rows match field2 = '2019#162440'. If a row that matches field2 = '2019#162440' is found, do NOT try to match field2 LIKE '%%2019#%%'
Also, no subqueries
Let me start by stating that I am not an expert in MySQL. It is not nearly as optimized as some other DBMSes, so the following query might not actually reduce your execution time. But it's worth a shot...
If field2 is indexed, this might be a really fast solution:
-- Get results where field2 = '2019#162440'
SELECT *
FROM table
WHERE field2 = '2019#162440'
-- Append...
UNION
-- Get results where field2 LIKE '%%2019#%%' but only
-- if there are no rows where field2 = '2019#162440'
SELECT *
FROM table
WHERE NOT EXISTS(
SELECT *
FROM table
WHERE field2 = '2019#162440'
)
AND field2 LIKE '%%2019#%%'
You are going to run your fastest query and select all results. Then, append the second, slower query which contains an EXISTS. The EXISTS clause will return true if the subquery contains any rows, which should short-circuit the entire second query and prevent it from runnning (thus appending 0 rows). If the first query returns 0 rows, however, then the second query will kick in and run the slower LIKE comparisons.
The best I can do is use the FOUND_ROWS() function along with a UNION:
SELECT *
FROM t
WHERE field2 = '2019#162440'
UNION ALL
SELECT *
FROM t
WHERE FOUND_ROWS() = 0
AND field2 LIKE '%%2019#%%'
SQL Fiddle Demo
I have two fields that I'm comparing with MySQL's function COALESCE(). For example, COALESCE(Field1, Field2). The problem is, Field1 is sometimes blank but not null; since it's not null COALESCE() selects Field1, even though its blank. In that case, I need it to select Field2.
I know I can write a if-then-else (CASE) statement in the query to check for this, but is there a nice simple function like COALESCE() for blank-but-not-null fields?
SELECT IFNULL(NULLIF(Field1,''),Field2)
NULLIF returns a NULL if Field1 is blank, while IFNULL returns Field1 if it's not blank or NULL and Field2 otherwise.
I know I'm late to the party here, but there is a way to do this while still using COALESCE(). This would then work if your value was NULL or ''.
Select COALESCE(NULLIF(Field1,''), Field2)
You can use a CASE expression:
CASE WHEN Field1 <> '' THEN Field1 ELSE Field2 END
Use CASE, for comparing both NULL and Blank.
SELECT CASE
WHEN Field1 IS NULL
OR LEN(LTRIM(RTRIM(Field1))) < 1
THEN Field2
ELSE Field1
END;
For comparing blank, do not use '', use LEN, LTRIM, RTRIM. Sometimes, blank may be with more than one spaces.
I can't find the answer since searching mysql NOT in google is a nightmare (even with the quotes).
I need to make a query like this:
SELECT * FROM table WHERE field=value AND field2!=value2 AND field3!=value3
How it is done? Is it even possible?
SELECT * FROM table WHERE
((field = value) AND
(field2 <> value2) AND
(field3 <> value3))
If you're dealing with NULL, you have to do two things:
Use SET ANSI_NULLS ON
Declare NULL values to a dummy value.
SQL Cannot compare nulls.
To do that:
SET #value = ISNULL(#value, -1);
Yes, you can do exactly what you wrote, but use <> instead of !=
Perhaps the answer depends on what "value" is? For example, for an integer 123 value would be 123; for a string "foobar" value would be 'foobar'.
have you tried the <> operator
SELECT * FROM table WHERE field = value AND field2 <> value2
have you tried "<>"? it works in Delphi