Remove order by of IN(1 2 10 5 5 )? - mysql

The following query works, but MySQL sorts the results set:
SELECT STRINGTEXT FROM WEBSTRINGS WHERE GUI=0 AND LANGID='GB' AND TOKENID IN
(312,47,48,49,50,51,52,53,54,55,56,57,58,60,61,62, 63,87,88,89,90,208,210,249,309,310,311);
This means that when I grab this in my recordset the data for TOKENID 312 is
at the end rather than being the first one, eg
I expected my resultset to come back in the following order of requests:
312,47,48,49,50,51,52,53,54,55,56,57,58,60,61,62,6 3,87,88,89,90,208,210,249,309,310,311
but it comes back as:
47,48,49,50,51,52,53,54,55,56,57,58,60,61,62,63,87 ,88,89,90,208,210,249,309,310,311,312
Is there anyway to get MySQL to not do this for this query? I really need
them to come back as is.

Try using 'ORDER BY FIELD`
SELECT STRINGTEXT
FROM WEBSTRINGS
WHERE GUI=0 AND LANGID='GB' AND TOKENID IN (312,47,48,49,50,51,52,53,54,55,56,57,58,60,61,62, 63,87,88,89,90,208,210,249,309,310,311)
ORDER BY FIELD (TOKENID,312,47,48,49,50,51,52,53,54,55,56,57,58,60,61,62,6 3,87,88,89,90,208,210,249,309,310,311)

SELECT STRINGTEXT
FROM WEBSTRINGS
WHERE GUI=0
AND LANGID='GB'
AND TOKENID IN (312,47,48,49,50,51,52,53,54,55,56,57,58,60,61,62,63,87,88,89,90,208,210,249,309,310,311);
ORDER BY FIELD(TOKENID,312,47,48,49,50,51,52,53,54,55,56,57,58,60,61,62,63,87,88,89,90,208,210,249,309,310,311);
field documentation: http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_field

Related

MySQL match area code only when given the full number

I have a database that lists a few area codes, area code + office codes and some whole numbers and a action. I want it to return a result by the digits given but I am not sure how to accomplish it. I have some MySQL knowledge but its not very deep.
Here is a example:
match | action
_____________________
234 | goto 1
333743 | goto 2
8005551212| goto 3
234843 | goto 4
I need to query the database with a full 10 digit number -
query 8005551212 gives "goto 3"
query 2345551212 gives "goto 1"
query 3337431212 gives "goto 2"
query 2348431212 gives "goto 4"
This would be similar to the LIKE selection, but I need to match against the database value instead of the query value. Matching the full number is easy,
SELECT * FROM database WHERE `match` = 8005551212;
First the number to query will always be 10 digits, so I am not sure how to format the SELECT statement to differentiate the match of 234XXXXXXX and 234843XXXX, as I can only have one match return. Basically if it does not match the 10 digits, then it checks 6 digits, then it will check the 3 digits.
I hope this makes sense, I do not have any other way to format the number and it has to be accomplished with just a single SQL query and return over a ODCB connection in Asterisk.
Try this
SELECT match, action FROM mytable WHERE '8005551212' like concat(match,'%')
The issue is that you will get two rows in one case .. given your data..
SELECT action
FROM mytable
WHERE '8005551212' like concat(match,'%')
order by length(match) desc limit 1
That should get the row that had the most digits matched..
try this:
SELECT * FROM (
SELECT 3 AS score,r.* FROM mytable r WHERE match LIKE CONCAT(SUBSTRING('1234567890',1,3),'%')
UNION ALL
SELECT 6 AS score,r.* FROM mytable r WHERE match LIKE CONCAT(SUBSTRING('1234567890',1,6),'%')
UNION ALL
SELECT 10 AS score,r.* FROM mytable r WHERE match LIKE CONCAT(SUBSTRING('1234567890',1,10),'%')
) AS tmp
ORDER BY score DESC
LIMIT 1;
What ended up working -
SELECT `function`,`destination`
FROM reroute
WHERE `group` = '${ARG2}'
AND `name` = 0
AND '${ARG1}' LIKE concat(`match`,'%')
ORDER BY length(`match`) DESC LIMIT 1

MySQL sorting with alphanumeric prefix

I've got a database with a column that contains the following data:
aaa-1
aaa-2
aaa-3
...
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
When I query and sort the data in ascending order, I get:
aaa-1
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
...
aaa-2
...
aaa-3
Is this actually the correct (machine) way of sorting? Is the order being screwed up because of the aaa- prefix? How do I go about sorting this the way a human would (ie something that looks like the first snippet)?
P.S. If the problem does lie in the prefix, is there a way to remove it and sort with just the numeric component?
P.P.S. It's been suggested to me that I should just change my data and add leading zeroes like aaa-0001 and aaa-0002, etc. However, I'm loathe to go that method as each time the list goes up an order of 10, I'd have to reformat this column.
Thank you all in advance! :)
You can extract the number part, convert it to numeric data type and then do an ORDER BY:
SELECT mytable.*,
CAST(SUBSTRING_INDEX(mycolumn, '-', - 1) AS UNSIGNED) mycolumnintdata
FROM
mytable
ORDER BY mycolumnintdata;
If there are expressions which does not match number, the CAST function would return 0 and those records would be displayed first. You may handle this separately if needed.
I had a similar issue and the trick that did it for me was this one
*"ORDER BY LENGTH(column_name), column_name
As long as the non-numeric part of the value is the same length, this will sort 1 before 10, 10 before 100, etc."*
as given by Andreas Bergström on this question.
Hope that helps someone.
this is the alphabetical order,
you want numerical order,
for do this you must in the ORDER BY clause
trim the costant "aaa-" part
convert it in number
convert(SUBSTRING(val, 3), integer)
I will give you a sample sorting. Not based on your data sample, but this could help you out.
Say you have data like this :
id
----
1
2
6
10
13
when you do ORDER BY id ASC would return :
id
----
1
10
13
2
6
I suggest, use LPAD.
This query : SELECT LPAD('12',5,'0') return 00012
So when you have table data like I provide above, you can sort them like this :
SELECT * FROM TABLE
ORDER BY LPAD(ID,7,'0') ASC
Based on your data.
SELECT SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3) return 100
So, SELECT LPAD( SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3), 7, '0') return 00000100
So you can combine string function such as SUBSTR and LPAD. Do have any clue now?

mysql select in aescending or descending order

my id or primary key is and the data type of is VARCHAR(50)
0.0.01
0.0.100
0.0.101
0.0.1011
0.0.201
0.0.501
0.0.99
0.0.999
0.01.0
0.01.10
0.02.10
0.02.20
0.02.99
01.0.0
01.0.99
01.02.99
01.03.444
01.05.88
10.02.99
100.100.100
25.45.1001
99.99.99
I have to get it in sorted order
so i tried this
select id from table order by cast(id as decimal) desc;
but it does not work
the expected order is after running the query
0.0.01
0.0.99
0.0.100
0.0.101
0.0.201
0.0.501
0.0.999
0.0.1011
0.01.0
0.01.10
0.02.10
0.02.20
0.02.99
01.0.0
01.0.99
01.02.99
01.03.444
01.05.88
10.02.99
25.45.1001
99.99.99
100.100.100
i am using mysql for this
Not an easier one but you can use substring_index for each decimal places
select *
from t
order by
substring_index(id,'.',1) * 1,
substring_index(substring_index(id,'.',-2),'.',1) * 1,
substring_index(id,'.',-1) * 1
Explanation
I have use substring_index what it does it will return the piece of string in provided column like in above case i have use id column by the occurrence of delimiter i.e(.) for example a string like 0.1.2 for above 3 sunstring_index usage will return as below
substring_index('0.1.2','.',1) will give result as 0
substring_index(substring_index('0.1.2','.',-2),'.',1) will give result as 1
substring_index('0.1.2','.',-1) will give result as 2
For type casting to number i have multiplied the result of substring_index to 1 so the order by expression will first order the results by the number before first dot then with number before second dot and last the number after second dot in ascending manner
Demo
Sources:
http://www.w3resource.com/mysql/string-functions/mysql-substring_index-function.php
Your ID column has invalid decimal value so casting could not work here.
Try without casting this should work:
select id from table order by id desc;
DEMO

MySQL database resultset with values as close to a number "x" as possible

Im trying to get a result set that contains the 10 values that are closest to, in this case, the number 3.
I have a database that has values in a column named rated which can be 1,2,3,4 or 5. What im trying to do is query the database and return the first 10 rows that have the values closest to 3. The values can be above 3 or below 3. I should note that these values in the rated column are floats.
I then need to sort these rows in order so that rows with value of 3 are first and then the row with lowest offset (+/-) from 3.
Is there any SQL query that can return atleast the result set of values closest to 3 ? or am i going to have to return the whole db and sort it myself?
To get the first 10 rows with highest value down i used the statement
SELECT * FROM tabs ORDER BY 5 DESC LIMIT 10";
5 refers to the column rated
Is there some way to modify this to do what i want ?
Thanks
If I understand your problem correctly, this should do the trick:
select *
from tabs
order by abs(`rated` - 3) asc
limit 10
Note that it sorts by the difference in ascending order, so those with a difference of 0 will come first.
SELECT * FROM tabs ORDER BY ABS(3 - Rate) ASC LIMIT 10
If I got right what you need try:
select *
from (
select
case when -(3-rated) > 0 then -(3-rated) else (3-rated) end as distance,
tabs.*
from tabs
) subsel
order by distance
limit 10

What is MySQL equivalent of MS Access LAST()?

I have no idea what the following query means as I do not have any exposure to MS Access. I need to convert it into MySQL and I can't figure out what does the LAST() does
SELECT
Containers.Container_No,
Max(Containers.LastOfContainer_Date),
Max(Containers.LastOfContainer_Time),
Last(Containers.LastOfETD),
Last(Containers.LastOfContainer_Status),
Last(Containers.LastOfPickup_From),
Last(Containers.LastOfPickup_To),
Last(Containers.LastOfConsignee_Name),
Last(Containers.LastOfContract_No),
Last(Containers.LastOfSeaLNo)
FROM
Containers
WHERE
Containers.Customer_Name = 'value here"
AND
Containers.LastOfContainer_Date >='Date here'
AND
Containers.LastOfContainer_Date <= 'Date here'
GROUP BY
Containers.Container_No
Can some one explain me what does this LAST() actually do?
UPDATE
It seems the query can be changed to following:
SELECT
Containers.Container_No,
Max(Containers.LastOfContainer_Date),
Max(Containers.LastOfContainer_Time),
Containers.LastOfETD,
Containers.LastOfContainer_Status,
Containers.LastOfPickup_From,
Containers.LastOfPickup_To,
Containers.LastOfConsignee_Name,
Containers.LastOfContract_No,
Containers.LastOfSeaLNo
FROM
Containers
WHERE
Containers.Customer_Name = '".$customername."'
AND
Containers.LastOfContainer_Date >='".$fromdate."'
AND
Containers.LastOfContainer_Date <= '".$todate."'
GROUP BY
Containers.Container_No
order by
Containers.LastOfETD,
Containers.LastOfContainer_Status,
Containers.LastOfPickup_From,
Containers.LastOfPickup_To,
Containers.LastOfConsignee_Name,
Containers.LastOfContract_No,
Containers.LastOfSeaLNo
The Last function returns the last value on a given query, for instance I assume that in the query you are using it would return the data of the last transaction made by that certain customer,
for instance lets say Mike Jagger bought 2things today, he will have 2 transactions with the same date, if you use the last function you would get the latest information that was inserted.
ID | Date | Product
1 12/12/2011 socks
2 12/12/2011 shirt
select last(product)
from buys;
it would output : shirt
BTW to emulate this behavior you might want to use
ORDER BY product DESC LIMIT 1
According to this page, "The Last function returns the last value from the result set of a query."
You might have to do some kind of ORDER BY x DESC LIMIT 1.