SQL How to sort by "Ranges" - mysql

I am currently using a database with different entries like dates, names, but also one column with time "ranges". This basically means that there can be a definite number like "10" in this cell, but also a value like "10-15" or "5-10".
So what I want to do here is to sort them by an "average" value ((Lowest+Highest)/2). So in case of the 3 mentioned values it should be
5-10
10
10-15
I am wondering if it is possible to embed this into the SQL statement in some way.
And if it is not possible, I'd like to know the easiest way to implement it otherwise.
Right now I am putting the $SQL_statement together via several conditions, then putting everything into $resultset which is then used with "while". Here are some snippets:
$resultset=mysql_query($SQL_statement);
while ($currententry=mysql_fetch_array($resultset))
{
echo $currententry['Platform'];
echo $currententry['PlaytimeInH']."h";
}

You can do this with substring_index() and arithmetic:
order by (substring_index(col, '-', 1) + 0 +
substring_index(col, '-', -1) + 0
) / 2
The division by 2 is unnecessary, but you do specify the average in your question.
Note that the above will work even if col has no hyphen in it.

You could use a select with a case when clause in order by
select col1, col2, cole
from your_table
order by case
when your_column = '5-10' then 1
when your_column = '10-15' then 2
when your_column = '15-20' then 3
else 4
end

Related

Is it possible to do a case..when..then in a where clause and assign it to a variable in MySQL?

For example, I'm defining this variable:
#test:= CASE WHEN #age < 21 THEN 'young' WHEN #age > 21 THEN 'old'
Fairly simple and straightforward. This would work when i do it in the SELECT clause, but somehow when i do it in the WHERE clause it gives 0 results, while i can define other variables like #test:= 1 without problems (returning the 10k+ results as expected).
I also tried it in multiple ways but it just doesn't seem to work with the case..when..then situation. Am i missing something or is this just not possible?
EDIT: Not the actual query, but an example of what i mean
SELECT
id, #test
FROM
ratetypes
WHERE
#test:=CASE
WHEN ratetype_id IN (1 , 2, 7, 9) THEN 'numeric'
ELSE 'non numeric'
END

MySQL ORDER BY CASE + operator

I am trying to do a search that would be sorted by relevance.
Let's say the search term contains 3 words: A, B and C. What I am trying to do is to check if the search term is present in the SELECT result and if yes that would increase its rank.
ORDER BY CASE
(
WHEN search_word_A_is_present THEN +1
WHEN search_word_B_is_present THEN +1
WHEN search_word_C_is_present THEN +1
ELSE 0
END
)
DESC
While there is no syntax error and the search runs and sorts by something (that seems different from what I want) but I am not sure what is being added up if anything. How would I go about seeing what the final rank (sum) is at the end for each result? Is this the correct way to do it?
Since in MySQL boolean conditions result in 1 and 0, you can simply add those up
ORDER BY search_word_A_is_present + search_word_B_is_present + search_word_C_is_present
DESC
A more practical example:
ORDER BY col1 = 1 + col2 = 'A' + col3 = 44 DESC

In MySQL, how to get all strings containing at least two characters in predefined list

I have a table of strings and want to retrieve all rows which contain at least two characters of a predefined list. Better explained by example:
My table looks like this:
Id|Word
1|Cat
2|Chicken
3|Dog
4|Elephant
5|Fish
6|Goat
This could be my char list:
Characterlist = 'A', 'B', 'C', 'E'
The select statement should then return:
Cat, Chicken, Elephant
I have looked through the string functions in mySQL docs but can not find a suitable function for doing this. Is it even possible?
Here's an inefficient way which relies on adding up the 0 or 1s returned by boolean conditions:
SELECT
*,
(
(UPPER(Word) LIKE '%A%') +
(UPPER(Word) LIKE '%B%') +
(UPPER(Word) LIKE '%C%') +
(UPPER(Word) LIKE '%D%')
) AS lettermatches
FROM yourtable
HAVING lettermatches >= 2
The LIKE conditions each return a 0 or 1. Those are added together, and the HAVING clause limits the result to the rows scoring 2 or greater.
It might be faster to use LOCATE() instead of LIKE:
SELECT
*,
(
(LOCATE('A', UPPER(Word)) > 0) +
(LOCATE('B', UPPER(Word)) > 0) +
(LOCATE('C', UPPER(Word)) > 0) +
(LOCATE('D', UPPER(Word)) > 0)
) AS lettermatches
FROM yourtable
HAVING lettermatches >= 2

Sort by Amount of True Booleans?

I tried to find this online, but I can't seem to find anything. How would I check for example, for the amount of integers (which = 1) and then sort the rows from most to least?
For example, these three booleans.
INT_ONE, INT_TWO, INT_THRE
Thank you :)
Add the columns together, and sort on that:
ORDER BY (INT_ONE + INT_TWO + INT_THREE) DESC
If you also need to use the value:
SELECT
(INT_ONE + INT_TWO + INT_THREE) AS num_true
FROM tbl
ORDER BY num_true DESC
This works because booleans in MySQL are 0 or 1.

MySQL get value and sum them up

I'm trying to sum up all the values in this script below but not sure how to do it, I searched the net and found array_sum() but not sure where to use it...
while($row = mysql_fetch_array($result))
{
$a = $row['aa'];
$b = $row['bb'];
$c = $row['cc'];
}
in the script above, all the variables has a value of either 1 or 0, I can manually add them by using $a + $b + $c but if the list gets longer it will take some time. Is there a faster way so that i can add up everything?
illustration of table
ID NAME AA BB CC
1 YOU 1 0 1
2 ME 1 1 1
So what i want is that "YOU" will have the value of 2 while "ME" will have the value of 3
SELECT (aa+bb+cc) AS yoursum FROM yourtable
According to your comment, I guess you want something like this:
SELECT SUM(aa), SUM(bb), SUM(cc) AS yoursum FROM yourtable
Or this, if there name column can have duplicates.
SELECT name, SUM(aa+bb+cc) AS yoursum
FROM yourtable
GROUP BY name
EDIT:
A slightly modified version of #cularis solution (in case you want only one value in the end):
SELECT (SUM(aa) + SUM(bb) + SUM(cc)) AS yoursum FROM yourtable
I myself would probably use one of the aggregate functions build into MySQL (most likely the SUM()). You can read more of these functions here: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html