Condition-check function in SQL SELECT (MySQL)? - mysql

I use MySQL and need to output in SQL SELECT a result of a function that is not uniform.
Namely,
if column A value is below certain number, I need to output one algebraic correction to column B inside the SELECT statement
and if it is above, another.
The algebraic corrections are not guaranteed to be "smooth" so I ought to have some IF-like statements.
I would like to do all these inline without e.g. creating custom functions. This is easier to maintain.
What would be the best syntax to achieve this "step" correction inside SELECT?

I think you can achieve this using a CASE expression. Something like this:
SELECT *,
CASE WHEN A < threshhold THEN exp_1(B) ELSE exp_2(B) END AS some_col,
FROM yourTable
Here you would replace exp_1(B) and exp_2(B) with the actual expressions involving column B.

Related

MySql extract data from a database based on two columns

I Am trying to create/run an my sql query in such a way that the sql selects data based a some conditions from Column 1 (USER) but at the same time Excludes some data, based on some conditions from column 2 (ADDRESS)
E.g.:
SELECT ADDRESS,USER
FROM Data1.Table1
WHERE FIELD(USER,'%AMIT%','%JOHN%','%SANDEEP%','%WARNE%')
AND ORIGINATING_ADDRESS NOT LIKE 'MUMBAI','CHINA','PAKISTAN'
This is giving error.Can some one please help ?
Use NOT IN to discard list of values from select. Considering that you want to discard when there is exact match
ORIGINATING_ADDRESS NOT IN ('MUMBAI','CHINA','PAKISTAN')
When you want to use pattern search and discard the use this
ORIGINATING_ADDRESS NOT LIKE '%MUMBAI%' OR
ORIGINATING_ADDRESS NOT LIKE '%CHINA%' OR
ORIGINATING_ADDRESS NOT LIKE '%PAKISTAN%'
For a set of values, use NOT IN, instead of NOT LIKE.
You might find regular expressions simpler for this purpose:
SELECT ADDRESS,USER
FROM Data1.Table1
WHERE USER REGEXP 'AMIT|JOHN|SANDEEP|WARNE' AND
ORIGINATING_ADDRESS NOT REGEXP 'MUMBAI|CHINA|PAKISTAN';

Match on substring

How would I do the following query? I want to match on the entire string minus the last two characters. In python it would be:
>>> x='hello'
>>> x[:-2]
'hel'
Conceptually, in SQL it would be:
select * from main_tmp where vid_country_key[:-2] = '2wh_gQwkMQg'
How would I actually do this? And would the above be faster or slower than:
select * from main_tmp where vid_country_key LIKE '2wh_gQwkMQg%'
As you probably know, the two example queries you provided are not equivalent, but altering the second like so should give the same results as the first, and might keep the potential indexing advantage of the second:
SELECT *
FROM main_tmp
WHERE vid_country_key LIKE '2wh_gQwkMQg%'
AND LENGTH(vid_country_key) = LENGTH('2wh_gQwkMQg')+2
;
If it doesn't use the index (assuming there is one), you could try changing AND to HAVING, or look into USE INDEX.
Oh, for the record, if you wanted to know the actual syntax for your first example, it would be
WHERE LEFT(vid_country_key, GREATEST(LENGTH(vid_country_key)-2, 0)) = '2wh_gQwkMQg'
the GREATEST is need in case the length of the field is less than 2.

Comparison of two strings in sql

I have written an ajax function to check if a value exists inside the database.
For example consider two strings "Book" and "book". In my current situation "Book" is there inside the Database and if I search using the query below
Select * from Cat where name='book'
OR
Select * from Cat where name like 'book'
It returns an empty result set since the 'b' is in lowercase. My collation is utf8_bin.
What will be the query to evaluate in such a way that it will be the same whether it is upper case or lower case.
Use upper() function to make both strings to upper case:
Select * from Cat where upper(name)=upper('book')
If I understand correctly you can use the upper or lower function in the comparison
Use LIKE instead =
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
Or also convert everythiing to upper/lower before comparing
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_upper

Is this a valid SQL conditional expression or a MySQL bug (feature)?

Trying to debug some joins which seemed to returning cartesian products I typed the equivalent ON condition into selects.
In MySQL
select *
from table
where columnname
seems to behave as though I typed where columname is not null. In joins typing on table.columnname is accepted but returns LOTS of rows. MySQL does the right thing if I correct it to on table1.column=table2.column but surely my first version was incorrect and illegal.
The contexts you are talking about, the WHERE clause and the ON clause in a join, simply accept an expression.
SELECT ...
FROM table1 JOIN table2 ON <expr>
WHERE <expr>
Expressions can include comparison operators like = but expressions can also be as simple as a single column or a single constant value.
Compare with another context that accepts an expression: the select-list.
SELECT <expr>, <expr>, <expr>
It's normal to use a single column as the expression in this context.
Most programming languages consider all values other than null, 0, '', false as true.
while(1); is an infinite loop irrespective of whether you give 1,2,'a',true and so on. I would say its a default expected behaviour and I have often used similar where clause

mysql calculation field stored in another field

I have fields in mysql that look like this:
constant1 , constant2, variable1, variable2, formula
The formula field stores a formula utilizing constant1, constant2, variable1 and variable2.
For example, the formula field may contain a calculation like this:
constant1 * variable1
When I use a select statement like this:
SELECT constant1, constant2, variable1, variable2, formula
FROM table
How do I retrieve the calculation result of constant1 * variable1 based on formula field?
You need one of two approaches:
Call some sort of eval function on your formula.
Parse the formula into an expression tree, substitute the values and then evaluate the expression.
Usig eval is quick to implement but dangerous because it could do something you didn't expect. Parsing the formula is best done with a dedicated library for building parsers, but if your expression syntax is very simple you could write a parser yourself without using a library. In either case SQL isn't the right language for this.
I'd recommend finding a library that can help you parse the formula. Whichever route you choose though, I'd suggest doing the bulk of the work on the database client, not in SQL. SQL was simply not designed for this sort of task.