why does sql = FALSE always return true - mysql

No matter what I do in MySQL 5.0 such as
SELECT 'test' = FALSE
SELECT '' = FALSE
I always get a 1 back in SQL. What is the reason for that? I was expecting a 0 or FALSE
EDIT adding context to the questions.
This is how the problem came about, it happened that $name inadvertently became false making this join always pass, then I wondered why this works.
SELECT a.id
FROM user a
INNER JOIN inventory b ON b.user_id = a.id AND b.name = $name

In MySql FALSE is a constant literal which is always evaluated as 0.
So you are checking if 'test' = 0 or if '' = 0, and since you are comparing a string with an integer, MySql will try to cast the string to an integer.
If you try this query:
SELECT 'test' = FALSE
it will return 1 (TRUE) because 'test' will be converted to 0, while if you try this:
SELECT '1test' = FALSE
it will return 0 (FALSE) because '1test' will be converted to 1.

This has to do with MySQL's implicit conversion when using comparison operators (i.e. =)
Taken from the docs:
Strings are automatically converted to numbers and numbers to strings as necessary.
So, in your case:
'test' gets converted to 0
FALSE is 0
0 = 0 is TRUE.

Related

SQLQuery - WHERE statement across two tables returns 0 results

Im running a query as per the below:-
unack_query = """
SELECT aa.AlertObjectID,
aa.TriggeredMessage,
aa.Acknowledged,
aa.AcknowledgedBy,
aa.TriggeredDateTime,
ao.EntityType,
ao.EntityCaption,
node.Caption,
node.CustomProperties.DeviceOwner
FROM Orion.AlertActive aa
INNER JOIN Orion.AlertObjects ao
ON aa.AlertObjectID = ao.AlertObjectID
LEFT JOIN Orion.Nodes node
ON ao.RelatedNodeId = NodeID
WHERE node.CustomProperties.DeviceOwner = 'Network Team' AND aa.Acknowledged = False
ORDER BY aa.TriggeredDateTime DESC
"""
This query returns zero results. However when I remove the "AND aa.Acknowledged = False" from the query it works. I can also see in the results that the Acknowledged field is a boolean. Can anyone point me in the right direction as to why this would fail?
Thanks
try checking if AND aa.Acknowledged = TRUE work as when you have not the condition ..(could be you have not false values)
or could be you have 'False' as a string
WHERE node.CustomProperties.DeviceOwner = 'Network Team' AND aa.Acknowledged = 'False'
or you could have no value so you should also check
WHERE node.CustomProperties.DeviceOwner = 'Network Team' AND aa.Acknowledged is null

SQL select where 1 value is greater then and chose one from the other 4

I want to check my database with the following condition:
$map = getData($conn, "SELECT * FROM map WHERE amt_cities > 4 AND neighbor_n = false OR neighbor_w = false OR neighbor_s = false OR neighbor_e = false");
So the query must only return something if there are more then 4 cities. and if at least one neighbor is false.
So if there is an entry in my database with the following values:
amt_cities = NULL
neighbor_n = false
neighbor_s = false
neighbor_w = false
neighbor_e = false
nothing must be returned.
but when i run my query now with the example given above it does return this entry... Any help is appreciated.
(i know the title is a bit confusing so if you know a better one pls don't hesitate to edit :)
I think you just need parentheses:
SELECT m.*
FROM map m
WHERE amt_cities > 4 AND
(neighbor_n = false OR neighbor_w = false OR neighbor_s = false OR neighbor_e = false);
You can actually use IN for this:
SELECT m.*
FROM map m
WHERE amt_cities > 4 AND
false in (neighbor_n, neighbor_w, neighbor_s, neighbor_e);
Or boolean logic:
SELECT m.*
FROM map m
WHERE amt_cities > 4 AND
not (neighbor_n AND neighbor_w AND neighbor_s AND neighbor_e);
Note: This might not be exactly the same if the neighbors could be NULL.

If Statement in MySQL Select

According to the docs a if works as follows:
IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3 [...].
Here is a random fiddle that represents my problem: http://sqlfiddle.com/#!9/8076e2/1
Basically what I'm trying to do is the following:
SELECT IF(whatever, 1, 2) FROM testing WHERE whatever = 'abc';
Since there is a record there that matches my WHERE clause it basically means that whatever won't be 0 or NULL like specified in the docs. Then why am I getting expression3 as result?
expr1 is intended to be a boolean expression, or at least a numeric value, not a string.
You can use true and false, or 1 and 0, etc. Technically any non-zero numeric value is interpreted as true.
Since you are using the string 'abc' as expr1, MySQL is implicitly converting that to the number 0, which represents false.
In order to return one result for a non-empty string, and another result for empty string or null, you can use a query like this:
SELECT if((whatever is not null and whatever != ''), 1, 2)
FROM testing
WHERE whatever = 'abc';
You can also do the same thing with CASE if you want to follow the SQL standard:
SELECT (case when (whatever is not null and whatever != '') then 1 else 2 end)
FROM testing
WHERE whatever = 'abc';
SELECT IF(COUNT(whatever) > 0, 1, 2) FROM testing WHERE whatever = 'abc';

When loading a csv file file how can insert negitive values as 0 and keep positive values

While reading over the docs I cannot seem to get this correct. I want to use load file to insert a CSV into a premade table. If one of the values is negative i want it to insert as 0. If not I want to keep its original value. I think im on the right track but i dont know.
ignore 1 lines (#item_name, #product_desc, #quanity_total) set item = #item_name, desc = #product_desc, quanity = if(#quanity_total < 0 then 0 else #quanity_total);
This part of your LOAD DATA INFILE statement could be written as:
ignore 1 lines
(#item_name, #product_desc, #quanity_total)
set item = #item_name,
`desc` = #product_desc,
quanity = if(#quanity_total < 0, 0, #quanity_total);
or by removing two not needed variables as:
ignore 1 lines
(item, `desc`, #quanity_total)
set quanity = if(#quanity_total < 0, 0, #quanity_total);
The column name desc is a reserved word and got to be quoted with backticks.
The IF syntax is as follows:
IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns
expr2; otherwise it returns expr3. IF() returns a numeric or string
value, depending on the context in which it is used.

mySQL query not returning correct results!

This query that I have is returning therapists whose 'therapistTable.activated' is equal to false as well as those set to true! so it's basically selecting all of the db, any advice would be appreciated!
` $query = "SELECT
therapistTable.*
FROM
therapistTable
WHERE
therapistTable.activated = 'true'
ORDER BY
therapistTable.distance
";
`
What is the column type of the column activated, and what are some sample values from the table?
Is it perhaps a BOOL, or some other integer value? 'true' is a string - TRUE is an integer.
If therapistTable.activated is a BOOL, you will want to compare it to TRUE instead of a string.
If you're using a boolean type, you should be using TRUE and not'true'. I don't know how that would cause your problem though..
Just to explain: TRUE is an integer (equal to 1), but 'true' is a string.
$query = "SELECT
therapistTable.*
FROM
therapistTable
WHERE
therapistTable.activated = 'true'
ORDER BY
therapistTable.distance
";
is correct