Where clause and truncating decimal points - mysql

Values in my table have a number of decimal points:
20.828292
21.9292992
...
I need to query the table to find results that match but to only 1 decimal place.
So where 20.8 would match the 20.828292.
Can I do this with SQL? How?

you can do it like
select * from table where TRUNCATE(attribute,1)=20.8

MySQL supports lots of comparison operators, like >= and <, which should be combined to get you the range you're looking for.

You can't do it by just comparing
select * from table where atribute = 20.8
In your case do a query like
select * from table where atribute >= 20.75 and atribute < 20.85
That should do it

Related

Select all records after the CHAR c

I have a column(char) with values between A and Z
I only want to select the records where the char is >= 'C'
Can anyone help me with this?
I tried >= 'C' but this didn't work. Also I couldn't find anything about this on the internet. So I thought it's a good question to ask.
You can use the ascii value for comparison.
select * from tablename where ascii(colname) >= ascii('C')
here is another method.
SELECT SUBSTRING_INDEX(YourColumn,'c',-1) FROM Yourtable;
Strings can be compared in MySQL with regular comparison operators, so this should work:
SELECT * FROM table WHERE col >= 'C'
Do note that the exact sort order (mainly case sensitivity) for strings depends on your characterset collation. Maybe that is the reason why it didn't work for you.
You can also use ASCII() function, which returns the character value of a single character, and compare those:
SELECT * FROM table WHERE ASCII(col) >= ASCII('C')
Note that this only works for single byte characters. For multi byte characters you must use ORD() instead of ASCII().
Yet another way is to use STRCMP() which compares two strings (again, using the sort order of your characterset collation) and returns 0 if the strings are the same, -1 if the first argument is smaller than the second, and 1 otherwise.
SELECT * FROM table WHERE STRCMP(col, 'C') >= 0

mysql using select with an arithmetic expression

I am trying to use select in combination with an arithmetic expression
SELECT * FROM `mytable` ORDER BY (`column1` / max(`column1`)* `column2`);
The problem is that it return a single row rather than all the rows sorted base on the expression.
Any idea?
Just order by column1. Dividing by a constant doesn't change the order.
EDIT: Add your expression as another field in the select. E.g.,
SELECT *, x+y as z ORDER BY z
See https://stackoverflow.com/a/10762333/2877364 for a full example of a similar situation.

having issue using and/or in mysql query

I'm having an issue thinking how to write the query i need.
I'll give you an example of what i'm trying to do..
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3=30||40
I need to select entries where clause1 must be exactly 10 and clause2 must be exactly 20, but clause3 can be 30 or 40 but have to be exactly 30 or exactly 40.
so it would select the entry if it were 10,20,30 or 10,20,40
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3=30 OR clause3=40 is incorrect though isn't it.
The numbers and clauses are only for example.
Thanks.
You've got to use parentheses 'round your last condition and use two times clause3=:
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND (clause3=30 || clause3=40)
or
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3 IN (30, 40)
Note
I would recommend to use the operator OR instead of ||, because in other SQL dialects (i.e. Oracle, Postgres) || is the concatenation operator. OR is standard SQL. So the first statement would better be written:
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND (clause3=30 OR clause3=40)
Explanation
The operator AND has got higher precedence than OR. Without parentheses the expression the following two statements would be evaluated the same
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3=30 OR clause3=40
SELECT * FROM table WHERE (clause1=10 AND clause2=20 AND clause3=30) OR clause3=40
and that is not what you want. So either put parentheses around your OR expressions or use the simpler IN().
Use IN.
SELECT *
FROM table
WHERE clause1=10 AND clause2=20 AND
clause3 in (30,40)

Filter results by the column length

I need to filter some results from a query where the field must have more than a given length.
I know that doesn't work, but it would be something like this:
SELECT * FROM MyTable WHERE COUNT(description) > 50
Is that doable or will I have to filter that on PHP(in my case) later?
Given you're using MySQL, you're probably looking for LENGTH().
For standard SQL, the function is called LEN().
If you are dealing with UTF-8, you will have to use CHAR_LENGTH() as LENGTH() measures the length in bytes while CHAR_LENGTH() will correctly measure the length in characters.
SELECT * FROM MyTable WHERE LENGTH(description) < 50
Use this query:
SELECT * FROM MyTable WHERE LENGTH(description) > 50
Read more: LENGTH function in mysql.
Reference: MySQL - How to select data by string length
Try This
SELECT * FROM MyTable WHERE LENGTH(description) > 50

mysql sort string number

I have a column of type varchar that stores many different numbers. Say for example there are 3 rows: 17.95, 199.95 and 139.95.How can i sort that field as numbers in mysql
Quickest, simplest? use * 1
select *
from tbl
order by number_as_char * 1
The other reasons for using * 1 are that it can
survive some horrendous mishaps with underflow (reduced decimal precision when choosing what to cast to)
works (and ignores) columns of purely non-numeric data
strips numeric portions of alphanumeric data, such as 123A, 124A, 125A
If you need to sort a char column containing text AND numbers then you can do this.
tbl contains: 2,10,a,c,d,b,4,3
select * from tbl order by number_as_char * 1 asc, number_as_char asc
expected output: 2,3,4,10,a,b,c,d
If you don't add the second order by argument only numbers will be sorted - text actually gets ignored.
Use a CAST or a CONVERT function.
This approach is helpful when sorting text as numbers:
SELECT `my_field`
FROM `my_table`
ORDER BY `my_field` + 0;
Found the solution on http://crodrigues.com/trick-mysql-order-string-as-number/.
Pad the string with leading zeroes:
ORDER BY LPAD(`column`,<max length of string>,"0")
If you really have to you can do this if your source data is compatible:
SELECT column FROM table ORDER BY CAST(column AS DECIMAL(10,2))
It's not going to be very fast for large data sets though. If you can you should change the schema to use DECIMAL in the first place though. Then it can be properly indexed for better performance.