Consider a table category in a database, with column typeis. The datatype is varchar with values
typeis
------
2.5.1
12
1.1.1
11
letters12
.........
I want to write a query that only returns records with "." and numbers from 0-9
For example
2.5.1
1.1.1
So far, I have
select typeis from category where typeis
not in
(select typeis from category where typeis REGEXP '[^0-9 \.]+')
and typeis in
(select typeis from category where typeis REGEXP '^[0-9]+[\.]')
which seems to work. The problem is that it takes over 3secs for just 1500 records. I would like to make it simpler and faster with just one REGEXP, instead of having nested select
Try: ^[0-9]+\.[0-9]+(\.[0-9]+)*
This should match things starting with a number(s) including a dot somewhere in the middle, and ending with numbers, and as many of these patterns as it would like.
This is pretty easy & powerful:
^([0-9]+\.*)+
The query-time issue could be caused by no indexing. Try to index typeis column - if it is possible create an index of its full length. For example if you've varchar(255) create the index of 255 length, like:
create index index_name on table_name (column_name(length))
Related
id text_1 text_2
1 おはよう おはよ
2 こんにちは ちわー
3 大丈夫 さよなら
4 でんわしたい でんわしよう
I have DB same above.
I want to search with input: おはよう大丈夫?
Expect result will match: id = 1 and id = 3.
Please help me how to query search in Mysql? Thanks you.
The following SQL query will fetch your 2 ids for exact string matching
select id from TABLENAME where text_1 in ('おはよう','大丈夫');
You can also use like operator with % to fetch approximately strings id.
You might face encoding issue (文字化け)depending on your DB settings, so you might have to convert SJIS <-> UTF-8
https://dev.mysql.com/doc/refman/5.7/en/faqs-cjk.html#faq-cjk-what-cjk-avail
Last but not least, if you want to use the full string as a comparison criteria to select the rows, then you can reuse the following code:
how to compute similarity between two strings in MYSQL
select id from TABLENAME where text_1 REGEXP "おはよう大丈夫"
I have data set of about 10K alphanumeric words with 10 characters length each. I need to match these using the first 3 characters and the last 3 characters.
Example: BGP12BR2010
In this case, I should use only BGP and 010 and see if there are any entries in my database. I have used
LEFT(replace(term_id,' ',''),3)||RIGHT(replace(term_id,' ',''),3)
Is there any other way to get this done.
You can also use LIKE:
SELECT * FROM yourTabel WHERE term_id LIKE 'BGP%210';
this matches on all string, not only 10 CHAR. to specify the lenght you can
use underscore
SELECT * FROM yourTabel WHERE term_id LIKE 'BGP____210';
A better way for this is to add 2 virtual persitent fields, where Mysql calculate the values and you also can set a index on it for a better performance and not using a full table scan
add persistent virtual fields
ALTER TABLE yourtable
ADD COLUMN first3 VARCHAR(5) AS (SUBSTRING('hallo',1,3)) PERSISTENT,
ADD COLUMN last3 VARCHAR(5) AS (SUBSTRING('hallo',-3,3)) PERSISTENT;
Now you can select it
SELECT * FROM yourTable where first in('BGP','YXZ','XXX) and last3 = '210';
I'll do so:
SELECT * FROM yourtable
WHERE LENGTH(yourcolumn) = 10
AND yourcolumn LIKE 'BPG%010';
To get all the values starting with 3 alphabets and ending with 3 numeric characters, use
select *
from t
where val regexp '^[a-z]{3}.+[0-9]{3}$'
To extract them, if they follow the above pattern,
select val, substring(val,1,3) as first3, substring(val,-3,3) last3,
--concatenate them if required
concat(substring(val,1,3), substring(val,-3,3)) concatenated_string
from t
where val regexp '^[a-z]{3}.+[0-9]{3}$'
Add a condition for length of the column if it has to be exactly 10 characters. In that case, change the regexp to '^[a-z]{3}.{numcharactersrequired}[0-9]{3}$' , which would be '^[a-z]{3}.{4}[0-9]{3}$'
SQL Fiddle
For example I have a table column : lorem,ipsum,mini,momo,testing
I need to select total 'Comma' 's count.
Is it possible to query it as result of : 4 because having 4 comma in the column ?
Does mysql have any trick like select character length by filtering certain words like :
SELECT CHAR_LENGTH(section,',') FROM table WHERE 1 = 1 ?
You are close. Just use subtraction:
select length(section) - length(replace(section, ',', ''))
However, you shouldn't be storing lists of things in strings. Instead, use a junction table with one row per item.
I need to find rows that contain a specific number in a set of numeric values that are stored in a table. I'm using the WHERE IN() function of mysql, but I'm having problems with the proper format.
Basically I have the following query:
SELECT id,category, text
FROM ws_cat
WHERE '11' IN (category)
The category field is a VARCHAR and looks like the following:
id category
1 11
2 12,11
3 1,13,9
So I need to find the rows with id 1 and 2 in this case. Unfortunately it doesn't work and I'm guessing it's because of the missing quotes, but all the ideas of reformating with QUOTES() or just changing the format of category to something like '12','11' wouldn't work either. Both would be possible for me as long as it works...
Use the FIND_IN_SET function:
SELECT id, category, text
FROM ws_cat
WHERE FIND_IN_SET('11', category) <> 0;
I have an LINK field in my table. Some rows have a link, some don't.
I'd like to select all rows where LINK is present. (length is greater than X characters).
How do I write this?
How about:
SELECT * FROM sometable WHERE CHAR_LENGTH(LINK) > 1
Here's the MySql string functions page (5.0).
Note that I chose CHAR_LENGTH instead of LENGTH, as if there are multibyte characters in the data you're probably really interested in how many characters there are, not how many bytes of storage they take. So for the above, a row where LINK is a single two-byte character wouldn't be returned - whereas it would when using LENGTH.
Note that if LINK is NULL, the result of CHAR_LENGTH(LINK) will be NULL as well, so the row won't match.
select * from [tbl] where [link] is not null and len([link]) > 1
For MySQL user:
LENGTH([link]) > 1
Try:
SELECT
*
FROM
YourTable
WHERE
CHAR_LENGTH(Link) > x
Just in case anybody want to find how in oracle and came here (like me), the syntax is
select length(FIELD) from TABLE
just in case ;)