MySQL - Order by number of chars - php

I have the most simple SQL
SELECT * FROM words
It has 13000 words (varchar). I need to get the longest word first in my output. I guess it might be possible with the WHERE command?
Alternative
If it doesn't work like that, is there a smart way to sort my output array so it will order it by the longest word first (in the 'word'-column). It looks like this (in the output loop)?
$output_array[$row['word']] = $row['another_word'];

Ordering the words by its length should do it:
SELECT *
FROM words
ORDER BY LENGTH(word) DESC

select * from words order by len(my_field) desc;

Related

How to find the nearest string value from table?

I have a table name "test" having a column code (string format) and data is like:
U298765
U298799
U210430
U210499
B239856
Now I want to get data by input field entry. If a user write U298750, I want show the nearest value U298765 and for U210401,U210430.
You can use the right function to fetch the number and then use following logic.
Select t.*
From test t
Order by abs(Right(code, length(code)-1) - Right(your_input, length(your_input)-1))
Limit 1
I am consodering that you need the nearest based on numbers only.
Try below query:
select code from test
order by abs(abs(substring(code,2,length(code)))-abs(substring('U298750',2,length('U298750'))))
Limit 1
In place of 'U298750' use your input
You seem to just want:
select t.*
from t
where code >= ? -- the input value
order by code desc
limit 1;
The ordering of strings alphabetically is sufficient for getting the "next" value after the string. There is no need to convert anything to numbers.

SORT Database by Parts of a String?

I do have a FIELD called CUSTOMERID in my Database which has values that look like this:
SFFG2443
The last four characters are always a four digit number which counts up, the first four characters are some random letters means it looks like that:
SFFG2443
FGTG2444
XDGG2445
...
I simply want to sort my table by only taking the LAST four digits(2443,2444,2445) into account. Is that possible with a single SQL-Statement? Thank you very much in advance.
SELECT * FROM yourtable
ORDER BY RIGHT(CUSTOMERID,4)
Should do the trick!
The Comment from Nobert van Nobelen works perfectly. The whole query looks like this:
SELECT customerid FROM customers ORDER BY substr(customerid,4,8) DESC LIMIT 0,1
Here is a link to the documentary for substr().

php mysql sort result by value of field using position of substring

I have field name "codehead" whose values are like
"53/066/10"
"54/066/05"
"56/066/09"
"52/069/15"
"53/069/02"
"67/069/02"
"00/020/80"
"00/020/98"
I want the results to be in following order
"00/020/80"
"00/020/98"
"53/066/05"
"53/066/10"
"54/066/09"
"52/069/15"
"53/069/02"
"67/069/02"
I have tried query like
$data= mysql_query("select codehead,sign, SUM(amt) as amt from pm where month='$pmmonth' and rc='R' GROUP BY substr(codehead,4,3) ASC,substr(codehead,7,2) ASC, sign ")
but could not get complete result.
This expression:
substr(codehead,7,2)
Is returning a forward slash and a digit. Looks like you wanted to start at position 8.
But that won't satisfy the order shown, the fifth row has 09, and that would come before 10 on the preceding row. The desired result shown makes it look like the result is also ordered on LEFT(codehead,2)
GROUP BY codehead, `sign`
ORDER BY substr(codehead,4,3) ASC
, LEFT(codehead,2) ASC
, substr(codehead,8,2) ASC
NOTE: In some future release of MySQL, the non-Standard MysQL extension of the GROUP BY to imply an ORDER BY may be removed.
I'm very suspicious of the GROUP BY on just the substrings. That's valid SQL to do that, I just question how you want to handle codehead values that match on the substrings, but is different in some other place... especially given that you're returning codehead in the SELECT list.
The normative pattern is to GROUP BY expressions in the SELECT list.
You can do something like this:
SELECT codehead,
substring(codehead,1,2) as 'one',
substring(codehead,4,3) as 'two',
substring(codehead,8,2) as 'three'
FROM foo
ORDER BY `two`, `one`, `three` ASC
SQLfiddle
and it will work, but it's going to have subpar performance because it's impossible to use indexes for that sort.
To address this you could add a new field to the table, eg sortkey, and insert a string in the format $two.$one.$three for each row. Then you can index and sort on that. Better yet, split all three values into their own fields and then you can index and sort however you want.

MYSQL select a piece of a string and order by that piece

I have a field with this kind of info "web-1/1.,web-2/2.,web-3/3.,web-4/4.,web-5/5.". Other registers could have different values like "web-1/4.,web-2/5.,web-3/1.,web-4/2.,web-5/3."
I want to select and order by lets say web-2/? would be web-2/1, web-2/2, web-2/3 and so on all fields that contain web-2 and order by the last number
I want to create a featured properties script different websites and specify feature number. Different properties, different websites different order
I would suggest that you look at the MySQL String Functions and more specifically the SUBSTRING_INDEX function. The reason I suggest this one over SUBSTRING is because the number before or after the slash might be more than a single number which would make the length of the first and/or second parts vary.
Example:
SELECT `info`,
SUBSTRING_INDEX(`info`, '/', 1) AS `first_part`,
SUBSTRING_INDEX(`info`, '/', -1) AS `second_part`
FROM `table`
ORDER BY `first_part` ASC,
`second_part` ASC;
Result:
Additional Example
In this example, I'm using CAST to convert the second part into an unsigned integer just in case it contains additional characters such as symbols or letters. In other words, the second part of "web-4/15." would be "15" and the second part of "web-4/15****" would also be "15".
SELECT `info`,
SUBSTRING_INDEX(`info`, '/', 1) AS `first_part`,
CAST(SUBSTRING_INDEX(`info`, '/', -1) AS UNSIGNED) `second_part`
FROM `table`
ORDER BY `first_part` ASC,
`second_part` ASC;
If the strings will always match the pattern you described, we can assume that the first value you want to sort on is index position 5 in the string (5 characters from the left). The second character is index 7. With that in mind, you can do the following, assuming that the string 'web-2/1' is in a field named field:
SELECT
`field`
FROM
`table`
ORDER BY
substr(`field`, 5, 1) ASC,
substr(`field`, 7, 1) ASC;
The substr() functions take the field as the first option, the index we mentioned above, and an option third parameter which is the count for how many characters to include starting from the second option.
You can tweak this as necessary if the string is slightly off, the main thing being the second option in the subtr() function.
Just add: ...ORDER BY SUBSTRING(username, 2) ASC
I would do this
SELECT info
FROM table
WHERE info LIKE 'web-2%'
ORDER BY info ASC

Search keyword with mysql

What should I use, to search for a keyword with mysql ?
I have a word and in the query I have
wordless
something word something
someword some
else
other
wordother
thingword
I want to output everything that has the word inside it, but the output to be like first outputed rows to be that rows with word as first letter on them, for example
wordless - will be the first because word are first characters of the word wordless
and the wordother to be outputed to to the first outputed rows, then after them to output something word something and etc, every word that contains the name word, but again to output first that rows that have the word at the first characters.
EDIT:
SELECT *,MATCH(add_songName) AGAINST('d' IN BOOLEAN MODE) asscoreFROM songs WHERE MATCH(add_songName) AGAINST('d') ORDER BYscoreDESC , Here i'm searching for d but it gives me an error -
Can't find FULLTEXT index matching the column list SELECT *,MATCH(add_songName) AGAINST('d' IN BOOLEAN MODE) as `score` FROM songs WHERE MATCH(add_songName) AGAINST('d') ORDER BY `score` DESC
Try to use Levenshtein algorithm in MySQL.
Levenshtein matching is a metric for measuring the amount of difference between two sequence, here it is strings. By default MySQL does not have this function, but you can write and add one.
Please take a look at the code here and add that code as a system function in MySQL, please see the example below on how to get the similarity of two strings.
Please see: https://github.com/rakesh-sankar/Tools/blob/master/MySQL/Levenshtein.txt
Example:
SELECT column1, LEVENSHTEIN(column1, 'matchme') AS perfectmatch FROM sometable ORDER BY perfectmatch DESC

Categories