mysql where clause field - mysql

I am trying to understand a piece of mysql code:
Select * from tableA
where type = 'blue'
and status = 'confirmed'
and statement
and date between '2017-01-01' and '2017-12-31'
Would would the "and statement" mean where statement is a field but without an =, or, and, >, < ect.
Thanks in advance

This is a MySQL peculiarity that other database engines do not exhibit. In other DBMS the equivalent would be:
and statement<>0

An empty where condition, as above, is effectively the same as AND LENGTH(statement) > 0. So any non-empty value in the statement column will be returned.

Related

MySQL Update query is not working with and operator

First query:
update tableA set columnA = 'Hello' and updated_at = now() where id = 10;
Second query:
update tableA set columnA = 'Hello', updated_at = now() where id = 10;
When I execute first query columnA updated as 0, where as second query worked fine and updated as Hello.
Why first query update the table as 0 value.
I think that MySQL's lax syntax is at work here. Consider rewriting your first update as:
UPDATE tableA
SET columnA = ('Hello' AND updated_at = NOW())
WHERE id = 10;
That is, the expression on the RHS being assigned to columnA is actually the AND of a string literal, and an assignment. Check the demo below to verify that this RHS in fact is evaluating to zero.
Demo
As to exactly why this is happening, we would have to lookup MySQL's rules for what happens. But best practice is to just stick with your second update query, which uses correct ANSI syntax.
#TimBiegeleisen is right, there is only one assigning expression for your first query.
MySQL syntax for SET assignment_list in UPDATE is like
assignment [, assignment] ...
every assigning expression for column separated by comma(,)
So, when you have multiple assignment use comma(,) to separate the assignment.
You found more details doc here

What causes a Date to be <null> while specifically selecting the NOT NULL fields?

In one of my MYSQL databases, I have done the following query :
SELECT * FROM mytable WHERE date_column < 1971-01-01 AND date_column IS NOT NULL;
This returns about 3000 items where the date is shown as NULL in the mysql command line prompt, and in the IntelliJ IDEA mysql database editor.
The column has a DATE type.
The database is a copy of a prod database environment that has been running for a couple years.
How can a record end up in that state ? What is that state ? How can I fix it properly ?
PS: The < 1971/01/01 clause was just to filter the correct dates out of the query. If I just query on the IS NOT NULL clause, I still get these weird dates of course.
I am surprised this works. First, date constants should be surrounded by single quotes:
SELECT *
FROM mytable
WHERE date_column < '1971-01-01' AND date_column IS NOT NULL;
Second, this cannot return a NULL value for date_column. This suggests one of two things:
date_col is actually another type, such as varchar and you are seeing 'NULL' the string, not NULL the value.
another column is NULL.

In MySQL select query, checking for a string or checking for true in a where clause?

Consider the following table:
SELECT id, Bill_Freq, Paid_From, Paid_To, Paid_Dt, rev_code FROM psr_20160708091408;
The requirement is to fetch the row which has rev_code populated with the string **SUM**.
I've also noticed that for every row with rev_code populated as **SUM** its Bill_Freq won't be either null or zero.
So I wrote two queries to fetch the row with the lowest id
Query based on string check in where clause:
select
min(id) as head_id,
bill_freq,
Paid_From,
Paid_To,
Paid_Dt
from
`psr_20160708091408` where rev_code = "**SUM**";
Query based on true condition:
select
min(id) as head_id,
bill_freq,
Paid_From,
Paid_To,
Paid_Dt
from
`psr_20160708091408` where bill_freq;
I haven't seen anyone use the second type, would like to know its reliability and circumstance of failure.
If by "second type" you mean a where clause with no explicit condition, then there is a good reason why you do not see it.
The SQL standard -- and most databases -- require explicit conditions in the where. MySQL allows the shorthand that you use but it really means:
where not billing_freq <=> 0
or equivalently:
where billing_freq <> 0 or billing_freq is null
(The <=> is the null-safe comparison operator.
The more important issue with your query is the min(). I presume that you actually want this:
select p.*
from psr_20160708091408 p
where rev_code = '**SUM**'
order by id
limit 1;
Also, you should use single quotes as string delimiters. That is the ANSI standard and there is rarely any reason to use double quotes.
Actually you can use the second type of query, but as your requirement is based on rev_code, it is always good to have condition with rev_code, because of 2 reasons
Bill_Freq having no NUlls or Zeros might be assumption based on current data
Even if it is true, in future, your application logic might change and it might have a scenario having NULL or zero, which will break your logic in future.
So my suggestion is to use first query with Rev_code
Please try to use below query
select
id,
bill_freq,
Paid_From,
Paid_To,
Paid_Dt
from
`psr_20160708091408` where rev_code = "**SUM**" ORDER BY ASC LIMIT 0,1;
Thanks.
The requirement says it itself.
The requirement is to fetch the row which has rev_code populated with
the string '**SUM**'
In the scenario that bill_freq IS NOT NULL and rev_code is populated with
the string '**SUM**' then your logic will obviously fail.
Go for
where rev_code = "**SUM**";

Use the result of the MySQL SELECT query as a WHERE condition in the same query

I'm trying to do this query:
SELECT MAX(`peg_num`)
AS "indicator"
FROM `list`
WHERE `list_id` = 1
AND "indicator" >= 1
But I'm getting the result of NULL. What I should be getting is 99, as the range of peg_num is 00 to 99.
The value checked against "indicator" should actually be a user input, so I want it to be versatile. But, it does give me the correct result if I flip the equality around:
SELECT MAX(`peg_num`)
AS "indicator"
FROM `list`
WHERE `list_id` = 1
AND "indicator" <= 1
Why would it do this?
Edit:
As suggested, I'm using the HAVING clause... but I just ditched the alias for now anyway:
SELECT MAX(`peg_num`) AS "indicator"
FROM `list`
GROUP BY `list_id`
HAVING MAX(`peg_num`) <= 40
Still very stubborn. It gives me 99 now no matter the value in the having clause, regardless of the inequality.
Edit2:
As a clarification:
What I want to happen is the query select the largest value in the range of peg_num, but only if it is larger than a user-given input. So, the max in this case is 99. If the user wants to select a number like 101, he/she can't because it's not in the range.
Because of double quotes, "indicator" in WHERE clause is interpreted as a string. Thus, it evaluates to 0, meaning it is always less than 1. Column names must be escaped in backticks.
Keep in mind that WHERE clause is executed before SELECT an hence aliases defined in SELECT can not be used in WHERE clause.
SELECT MAX(`peg_num`) AS `indicator`
FROM `list`
WHERE `list_id` = 1
HAVING `indicator` >= 1
You might want to check out the link on the answer to another Stack question about not being allowed to use alias in where clause:
Can you use an alias in the WHERE clause in mysql?
Paul Dixon cites:
It is not allowable to refer to a column alias in a WHERE clause,
because the column value might not yet be determined when the WHERE
clause is executed. See Section B.1.5.4, “Problems with Column
Aliases”.
Also:
Standard SQL disallows references to column aliases in a WHERE clause.
The behavior you're seeing in your query when you swap the '<=' and '>=' operators, results from the query comparing the string/varchar 'indicator' to the number 1.
That's why you see the correct answer..when ('indicator' >= 1) which is true, and null when ('indicator' <= 1) which is false.
I don't know, but I'm amazed either of them work at all. WHERE works serially on fields belonging to individual records and I wouldn't expect it to work on "indicator" since that's a group calculation.
Does this do what you want?
SELECT max(`peg_num` ) AS "indicator"
FROM actions
WHERE `peg_num` >=1
AND `list_id` <= 1
WHERE happens before SELECT, and don't know what's "indicator".
You should use HAVING (with GROUP BY) to use the SELECT fields
Here's the documentation for syntax
http://dev.mysql.com/doc/refman/5.5/en/select.html
Something like this is the idea
SELECT MAX(peg_num) AS indicator
FROM list
WHERE list_id = 1
HAVING indicator <= 1
I can't test it and i never met Mysql so just the idea,
You should use HAVING
No quotes in HAVING condition
This must work:
SELECT MAX(peg_num)
AS indicator
FROM list
WHERE list_id = 1
HAVING indicator >= 1
I completely re-invented my query and it worked. The thing is, I had to use a nested query (and I wanted to not do that as much as possible, my professor had always discouraged it).
Anyway, here it is:
SELECT IF(`key` < 900, `key`, null) `key`
FROM (
(
SELECT MAX( `peg_num` ) AS `key`
FROM `list`
WHERE `list_id` =1
) AS `derivedTable`
)

MySQL strange behaviour for comaprison operator (!=/<>) i.e. NotEqualTo

I am using simple WHERE clause to fetch records from a table using NotEqualTo comparison. Ironically it doesn't return recrods where column has no value at all.
Query
SELECT * FROM TABLE WHERE COL<>'Something'
Above query will return all records where COL isNotEqual to Something, however, It doesn't return those where COL is empty or NULL.
Why is that so?
It should return records where COL is empty/null as it still satisfies the condition that it isn't equal to 'Something'
Okies thanks guys for helping me out. I didn't know about this behaviour. For others to handle this issue, I modified query as below to get required results:
SELECT * FROM TABLE WHERE COL IS NULL OR COL<>'Something'
Check Manual for arithmetic comparison.
You cannot use arithmetic comparison operators such as =, <, or <> to test for NULL.
Check SQL Fiddle