How to find min() value in MYSQL - mysql

I have in my DB a column of varchar type.
Table name: Transcations
Column name :authprocess
I want to find min() value on the varchar column.
authprocess
A1D1
A1D3
A1D4
A1D1
A1B1
A1D5
......
i am using the command
select min(authprocess) from Trnascation
then it give the "0000" value
Please tell me the command in Mysql.

MIN() makes no real sense for a character string, your best bet is to do a string sort by ascending values and select the top one perhaps??
SELECT TOP 1 FROM table ORDER ASC;

MIN() takes the first entry after sorting. For text columns, the sorting order is ascii order, so for alpha-numeric data it's 0..9a..zA..Z
You must have an entry with the value 0000, which will of course be the minimum.
It sounds like you need to restrict your search to "valid" entries. Assuming entries are valid if they have form letter-digit-letter-digit, try this:
select min(authprocess)
from Transcations
where authprocess regexp '[A-Z]\d[A-Z]\d'
You can adjust the regex to suit whatever your needs are - eg perhaps it's A-digit-D-digit, which would match the regex 'A\dD\d'

Related

MySQL returns all rows when field=0 from SECOND Select query

This case is similar to: S.O Question; mySQL returns all rows when field=0, and the Accepted answer was a very simple trick, to souround the ZERO with single quotes
FROM:
SELECT * FROM table WHERE email=0
TO:
SELECT * FROM table WHERE email='0'
However, my case is slightly different in that my Query is something like:
SELECT * FROM table WHERE email=(
SELECT my_column_value FROM myTable WHERE my_column_value=0 AND user_id =15 LIMIT 1 )
Which in a sense, becomes like simply saying: SELECT * FROM table WHERE email=0, but now with a Second Query.
PLEASE NOTE: It is a MUST that I use the SECOND QUERY.
When I tried: SELECT * FROM table WHERE email='( SELECT my_column_value FROM myTable WHERE my_column_value=0 LIMIT 1 )' (Notice the Single Quotes on the second query)
MySql SCREAMED Errors near '(.
How can this be achieved
Any Suggestion is highly honored
EDIT1: For a visual perspective of the Query
See the STEN_TB here: http://snag.gy/Rq8dq.jpg
Now, the main aim is to get the sten_h where rawscore_h = 0;
The CURRENT QUERY as a whole.
SELECT sten_h
FROM sten_tb
WHERE rawscore_h = (
SELECT `for_print_stens_rowscore`
FROM `for_print_stens_tb`
WHERE `for_print_stens_student_id` =3
AND `for_print_stens_factor_name` = 'Factor H' )
The result of the Second Query can be any number including ZERO.
Any number from >=1 Works and returns a single corresponding value from sten_h. Only =0 does not Work, it returns all rows
That's the issue.
CORRECT ANSWER OR SOLUTION FOR THIS
Just in case someone ends up in this paradox, the Accepted answer has it all.
SEE STEN_TB: http://snag.gy/Rq8dq.jpg
SEE The desired Query result here: http://snag.gy/wa4yA.jpg
I believe your issue is with implicit datatype conversions. You can make those datatype conversions explicit, to gain control.
(The "trick" with wrapping a literal 0 in single quotes, that makes the literal a string literal, rather than a numeric.)
In the more general case, you can use a CAST or CONVERT function to explicitly specify a datatype conversion. You can use an expression in place of a column name, wherever you need to...
For example, to get the value returned by my_column_value to match the datatype of the email column, assuming email is character type, something like:
... email = (SELECT CONVERT(my_column_value,CHAR(255)) FROM myTable WHERE ...
or, to get the a literal integer value to be a string value:
... FROM myTable WHERE my_column_value = CONVERT(0,CHAR(30)) ...
If email and my_column_value are just indicating true or false then they should almost certainly be both BIT NOT NULL or other two-value type that your schema uses for booleans. (Your ORM may use a particular one.) Casting is frequently a hack made necessary by a poor design.
If it should be a particular user then you shouldn't use LIMIT because tables are unordered and that doesn't return a particular user. Explain in your question what your query is supposed to return including exactly what you mean by "15th".
(Having all those similar columns is bad design: rawscore_a, sten_a, rawscore_b, sten_b,... . Use a table with two columns: rawscore, sten.)

MySQL Unexpected Result from "in (' ' or ' ')"

What I'm Using: The most recent MySQL on Ubuntu 12.
The Set Up: Suppose I have a table "EmployeePayment" with "Name" and "Hours" for each employee. Suppose I already have it populated with values.
The Question: When I use the command
select * from EmployeePayment where Name in ('');
I get the empty set, as I'd expect. But, when I use
select * from EmployeePayment where Name in ('' or '');
I get the entire table returned. Moreover, if I'm picky and put in the command
select Name, SUM(Hours) from EmployeePayment where Name in ('' or '');
then it only returns whatever is the top name from the table. What's happening with this "in" command?
First off, you need to get rid of the or, the proper syntax for the in clause uses commas to separate the possibilities, such as:
sql> select name from people where status in ('intelligent', 'good looking')
pax
1 row returned
What your current variant is doing is applying the or operator to give you a one-element in-list. See here for more detail.
The reason why you're only getting one row for the aggregated query is because you have no group by clause, so you're grouping all rows. Most DBMS' would then complain about having a non-aggregated column that isn't part of the grouping, but MySQL is a bit fancy-free and footloose with the rules in that regard.
It's obviously grouping over the whole table (as it should) but applying some default aggregating function to the name (which it probably shouldn't, but does according to its documentation).
This MySQL extension is covered here but heed the warning: MySQL can choose any of the myriad possible values for these non-aggregated, non-group-by columns, so it's more useful when you know that all the rows in a given group share the same value for the column.
You're effectively doing this:
select * from EmployeePayment where Name in (0);
The OR expression evaluates to 0, and WHERE Name IN (0); returns all rows. You have to use the proper IN syntax as suggested in the other answers:
SELECT * FROM EmployeePayment WHERE Name IN ('foo', 'bar');
IN uses comma separated values, for example: WHERE Name IN ('tim','beth')
So try WHERE Name IN ('','');
But more importantly, why would you want to check where a value is empty or empty? Or was that just to get the question across?
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in

sql query help in sorting a field that's got both string and numbers

I've got a text field called source_recid. It stores half string half number like strings in it.
Example
shop.orders.32442
the syntax is DATABASENAME.TABLENAME.RECID
My goal is to scan this col and find out the biggest RECID ( the integer) in it.
So, in a case like this
shop.orders.32442
shop.orders.82000
shop.orders.34452
It would be the record whose source_recid is shop.orders.82000. Why? Cause 82000 happens to be the largest integer.
What SQL statement would get me that record?
One option to this is to create a new column ( the_ids ) and move all the integers in it and then run something like this
select source_recid from mytable
where source_recid like 'shop.orders.%'
order by the_ids DESC
LIMIT 1
Is there a way to pull this off without going thru this step?
First of all, unless all of your RECIDs are exactly five characters long forever and always, the select you put up won't work, becuase "shop.order.9" would come out as larger than "shop.order.10", which is wrong.
What I think we need to do here is extract the numeric part, cast it to an integer, and sort by that. Now, I don't have access to mySQL, so this may not be exactly right, but it should be close ...
SELECT
CAST(SUBSTRING_INDEX(RECID,'.',-1) AS INT) AS RecIdNumber
FROM
table
WHERE
RECID LIKE 'shop.order.%'
ORDER BY
RecIdNumber DESC
LIMIT
0, 1
This will take the part after the last dot, convert it to an INT, name it 'RecIdNumber', and sort by that.
I hope this helps.
SELECT CAST(SUBSTRING_INDEX(field,'.',-1) AS INT) AS RID
FROM yourtable
WHERE
RECID LIKE 'shop.order.%'
ORDER BY
RID DESC

Sorting an Alphanumeric value mysql

I want to find the max value in a column.
Column values are,
E00004,
A00005,
B00011,
H-00001,
E2100112,
EFQ20098,
ESSF20003
I just want to sort the values by their number, Dont mind about the alphabets. It have to be like this, I'm using MYSQL
E2100112,
ESSF20003,
EFQ20098,
B00011,
A00005,
E00004,
H-00001
Assuming the last 5 digits are the number:
select columnName from tableName
order by convert(int, right(columnName, 5)) desc
As #IkeWalker stated, the number can have an arbitrary size.
For it, you'll have to use a while cycle to check the number.
Or, you can have a function do that for you!
Check this article!

Weird Results on MySQL SELECT Statement with ORDER BY

I'm getting weird results on a MySQL SELECT statement that uses ORDER BY my_column ASC.
It's ordering the results the way a "computer" would order them, instead of a human:
Item F: 241.565853
Item B: 25.310854
Item D: 25.397155
Item C: 260.252356
Item A: 27.7740
Item E: 271.774058
How do I get it to ORDER BY in the correct manner? My SELECT statement has a couple LEFT JOINS-- not sure if that would make a difference.
Any suggestions on how to correct this problem?
Something like this should do it:
ORDER BY ABS(my_column) ASC
The column you're ordering by is most likely a string-based (varchar, text, etc) datatype. You're seeing lexically-correct ordering for such a datatype. Change the column to use a numeric datatype, or (less-preferably, because why are you storing a numeric value as a string) cast the column to a numeric type and perform the sort on that cast.