Is there a way to ORDER by REGEXP in MySQL statement? - mysql

what I have
SELECT CONCAT(`names`,' ',`office`) `bigDataField`
FROM `item_table`
HAVING `bigDataField` REGEXP "jessy|c";
returnes also Data which just contains letter "c" so I would like to ORDER BY most same matching characters, is that possible ?
NOTE: words and characters get changed by user input. So it can be only one character or a few or even a few words.
sql fiddle http://sqlfiddle.com/#!2/dc87e/1
Thanks for all the help

You can order by any expression.
regexp returns the number of matches for the specified regex
So, this:
order by `bigDataField` regexp 'c' desc
will order your data by the bigDataField that has the most c's in it as first so I guess it's not what you are looking for. You can use multiple CASE-WHENs to check the length of the pattern matching (warning: bad performance - not recommended for big tables)
try this
SELECT CONCAT(`names`,' ',`office`) `bigDataField`,
CASE WHEN CONCAT(`names`,' ',`office`) regexp 'jessy' > 0 then length('jessy') * (CONCAT(`names`,' ',`office`) regexp 'jessy') ELSE
CASE WHEN CONCAT(`names`,' ',`office`) regexp 'c' > 0 then length('c') * (CONCAT(`names`,' ',`office`) regexp 'c') ELSE 0 END
END as charmatchcount
FROM `item_table`
HAVING `bigDataField` REGEXP "jessy|c"
ORDER BY charmatchcount desc
To avoid the above ugliness you must use an external library, or create your own function. You may find this thread helpful MySQL - Return matching pattern in REGEXP query

You can try with this
SELECT CONCAT(`names`,' ',`office`) `bigDataField`
FROM `item_table`
HAVING `bigDataField` REGEXP '[a-z] c' order by bigDataField asc;
Hope this will work for you

this works:
SELECT col, IF( LOCATE('Jessy', col) = 0, 0 , LENGTH('Jessy')) as ord
FROM
(
SELECT CONCAT( `names`,' ',`office`) `col`
FROM `item_table`
HAVING `col` REGEXP "jessy|c"
) x
ORDER BY ord DESC;
but if there is 3 word in REGEXP, hard to find relevant query.
BTW are you looking for Full Text Search?

Related

MySQL Query Question to finde value in string

i have a simple (i hope its simple) question.
In my database, i have an entry like this:
Now, i need a response with true or a count if the '514' in 'error_code' is in the string 'count_alarm'. In this example it returns zero because 514 isnt in count_alarm.
I beginns the query, but i dont know how i can solve this query:
select count(*) from table where sID='56df32a1463d4387' and [if
error_code in count_alarm then True]
Somebody an idea?
Perhaps you can just use REGEXP here:
SELECT COUNT(*) AS cnt
FROM yourTable
WHERE sID = '56df32a1463d4387' AND
count_alarm REGEXP CONCAT('[[:<:]]', error_code, '[[:>:]]');
find_in_set can parse comma separated fields:
select count(*)
from your_table
where sID = '56df32a1463d4387'
and find_in_set(error_code, replace(count_alarm, '|', ',')) > 0
or use instr
where sID = '56df32a1463d4387'
and instr(count_alarm, concat('|', error_code, '|')) > 0
select count(sID) as n514 from table where sID='56df32a1463d4387'
and count_alarm like '%|514|%'
The LIKE operator searches for the pattern |514| anywhere in the value under count_alarm.
The assumption is that the first and last character of that value is this character | else 514 would not be found if it is the first or last pattern within that value.

How can I SELECT rows that are not number

How can I select rows in MySQL that are not numbers?
SELECT *
FROM `TABLE`
WHERE `Column_B` <> Number
I know, this is a stupid question, but I haven't found a solution for this. Column_B is varchar(3).
Try this sqlfiddle..
http://sqlfiddle.com/#!2/17f28/1
SELECT * FROM Table1 WHERE col NOT REGEXP '^[0-9]+$'
If you want to match real numbers (floats) rather than integers, you need to handle the case above, along with cases where your pattern is between 0 and 1 (i.e. 0.25), as well as case where your pattern has a decimal part that is 0. (i.e. 2.0). And while we're at it, we'll add support for leading zeros on integers (i.e. 005):
^0*[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*$
sql looks like SELECT * FROM Table1 WHERE col NOT REGEXP '^0*[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*$'
This is one way of doing that, using mysql regexp operator (fiddle):
SELECT * FROM `TABLE`
WHERE `Column_B` NOT REGEXP '^-?[0-9]*\\.?[0-9]+([Ee][+-][0-9]+)?$';
Supports signed/unsigned integers, decimals, and scientific notation.
Note: it is recommended to avoid using sql keywords for entity names, even if you quote them with backticks.
this also will work for you
SELECT col FROM Table1
WHERE col NOT IN ( SELECT col FROM Table1
where CAST(col as DECIMAL(10,5)) !=0
or Col = '0' )
DEMO

MySQL and word boundary in regex

How can I use regex word boundary in a MySQL query?
For instance, I want to match either 'product' or 'newsletter',
It works fine with OR,
IF(? = 'product' OR ? = 'newsletter', ... , ...)
But how about a regex? I assume it would be something like this below?
IF(? REGEXP '^('product'||'newsletter')+$', ..., ... )
SELECT *
FROM tbl
WHERE description REGEXP '[[:<:]]red[[:>:]]|[[:<:]]blue[[:>:]]'
or, as a select expression:
SELECT IF(description REGEXP '[[:<:]]red[[:>:]]|[[:<:]]blue[[:>:]]', 1, 0) AS is_matched, tbl.*
FROM description
You can use RLIKE as a synonym but REGEXP seems to be more popular [citation needed].
Case Sensitivity
From the MySQL manual for REGEXP:
REGEXP is not case sensitive, except when used with binary strings.
mysql> SELECT 'a' REGEXP 'A', 'a' REGEXP BINARY 'A';
-> 1 0
Use REGEXP or RLIKE.
Regular expressions in MySQL
In your case, you could use MATCH() ... AGAINST fulltext search with MyISAM storage engine.
Or you could use IN() as #Wiseguy mentioned

MySQL. I stink at RegExs. Just need one to tell me if string starts with a numeral

I need a MySQL query w/ Regex to tell me if my string's first character is a number from 0 to 9.
The following query returns '1', since the REGEXP matches. You can adapt it for your purposes:
SELECT '123 this starts with a digit' REGEXP '^[[:digit:]]';
You can use it in a SELECT like this:
SELECT * FROM tbl WHERE field REGEXP '^[[:digit:]]';
Use this:
SELECT 'a12' REGEXP '^[0-9]';
=> 0
SELECT '4ab' REGEXP '^[0-9]';
=> 1

MySQL - If It Starts With A Number Or Special Character

SELECT *
FROM `thread`
WHERE forumid NOT IN (1,2,3) AND IF( LEFT( title, 1) = '#', 1, 0)
ORDER BY title ASC
I have this query which will select something if it starts with a #. What I want to do is if # is given as a value it will look for numbers and special characters. Or anything that is not a normal letter.
How would I do this?
If you want to select all the rows whose "title" does not begin with a letter, use REGEXP:
SELECT *
FROM thread
WHERE forumid NOT IN (1,2,3)
AND title NOT REGEXP '^[[:alpha:]]'
ORDER BY title ASC
NOT means "not" (obviously ;))
^ means "starts with"
[[:alpha:]] means "alphabetic characters only"
Find more about REGEXP in MySQL's manual.
it's POSSIBLE you can try to cast it as a char:
CAST('#' AS CHAR)
but i don't know if this will work for the octothorpe (aka pound symbol :) ) because that's the symbol for starting a comment in MySQL
SELECT t.*
FROM `thread` t
WHERE t.forumid NOT IN (1,2,3)
AND INSTR(t.title, '#') = 0
ORDER BY t.title
Use the INSTR to get the position of a given string - if you want when a string starts, check for 0 (possibly 1 - the documentation doesn't state if it's zero or one based).