I have a table in MySql, the table has 3 fields (name, date, number). The table connected to my app via PHP.
What I need is to make MySql to accept only English numbers. I don't want to do it via PHP, is there any way to do it in MySql to restrict it from accepting such as (Arabic, Parisian).
If there's any options can I do it from MySql.
Thanks
You can verify that easily with a regex,
SELECT * FROM yourTable WHERE number REGEXP '^[0-9]*$';
Edit : If your goal is only to block Arabic text, then here a regex that would select only ASCII character from the space to the tild(0-127).
SELECT * FROM yourTable where number REGEXP '^[ -~]*$'
Related
I have a field called EMAIL_ADDRESS. One of the records would be:
john#gmail.com, mike#gmail.com, joe#yahoo.com, george#yahoo.com, fred#gmail.com
I wan to remove all yahoo addresses in my SELECT query to get:
john#gmail.com, mike#gmail.com, fred#gmail.com
If I use
REPLACE(SM.SCORECARD_EMAIL_ADDRESS, 'joe#yahoo.com,', '')
this works.
If I want to remove ALL yahoo email addresses this doesn't work:
REPLACE(SM.SCORECARD_EMAIL_ADDRESS, '%#yahoo.com,', '')
because wildcards don't seem to work as it's looking for % in the string.
You should probably fix your table design and stop storing CSV lists of email addresses. Instead, get each email onto a separate record. As a short term fix, if you're running MySQL 8+, you may use REGEXP_REPLACE():
UPDATE yourTable
SET EMAIL_ADDRESS = REGEXP_REPLACE(
REGEXP_REPLACE(EMAIL_ADDRESS, '(, )?\\S+#yahoo\\.com,?', ','), '^,+|,+$', '')
WHERE EMAIL_ADDRESS LIKE '%#yahoo.com%';
If you don't need to udpate records but you want them only in the SELECT query you can use NOT LIKE operator
SELECT * FROM your_table WHERE email NOT LIKE '%yahoo.com'
So you get records that doesn’t match the like pattern
I want to find usernames with reduplicated words in my database
Below is my SQL statement:
SELECT * FROM users WHERE BINARY `name` regexp 'a{2}|b{2}|c{2}|d{2}|e{2}|f{2}|g{2}|h{2}|i{2}|j{2}|k{2}|l{2}|m{2}|n{2}|o{2}|p{2}|q{2}|r{2}|s{2}|t{2}|u{2}|v{2}|w{2}|x{2}|y{2}|z{2}'
I guess there should be an easier way, but I don't know how to do it.
I also want to find usernames like 'ABAA' or 'AABA', but I won't write this SQL or say this regex
As MySQL's regex engine does not support backreferences, your approach is reasonable. To simplify your query, you could maintain a table of double letters:
table: letters (val)
aa
bb
...
zz
Now your query can be simplified to:
SELECT *
FROM users u
WHERE EXISTS (
SELECT 1
FROM letters l
WHERS INSTR(u.name, l.val) > 0
);
Try:
create table users(name varchar(100));
insert into users (name) values
("abaa"),
("bba"),
("abc"),
("aCCa");
SELECT * FROM users WHERE LOWER(name) REGEXP 'aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu|vv|ww|xx|yy|zz';
Prints:
abaa
bba
aCCa
i think i found the answer,I upgraded my MySQL database to MariaDB.
this is my sql statement:
SELECT * FROM users WHERE `name` REGEXP '([a-z])\\1'
This is the sql statement to query like 'aaba':
SELECT * FROM users WHERE `name` REGEXP '([a-z])\\1((?!\\1)[a-z])\\1'
Note: Because MariaDB uses the C escape syntax in strings (for example, "\n" to represent the newline character), you must double any "" that you use in your REGEXP strings.
In MySQL 8.0
WHERE name REGEXP '([a-z])\\1'
Let the collation take care of ignore case.
I'm trying to SELECT all rows that have an hyphenated word in a certain column. This is what I have so far:
SELECT * FROM MyTable WHERE list RLIKE '\w-\w'
But it's only returning 1 row, when I know there are a lot more. Any ideas why?
Thank you
| List |
built-in
self-discipline
hang-up
....
EDIT: Not sure if it matters, but list is utf8_unicode_ci
MySQL regular expressions don't support \w (or any other escape sequences for character classes). You must write:
SELECT * FROM MyTable WHERE list RLIKE '[[:alnum:]_]-[[:alnum:]_]'
See the Documentation for details of MySQL regular expressions.
if you want to try and use it without regex then try the wildcard.. not sure if you are not able to use that or not, but that should work too
SELECT * FROM MyTable WHERE list LIKE '%-%'
Ok, so here is the issue.
I have a table with some columns and 'subject' is one of the columns.
I need to get the first 10 letters from the 'subject' field no matter the 'subject' field contains a string with 100 letters.
For example,
Table - tbl.
Columns - id, subject, value.
SQL Query:
SELECT subject FROM tbl WHERE id ='$id';
The result I am getting is, for example
Hello, this is my subject and how are you
I only require the first 10 characters
Hello, thi
I can understand that I can remove the rest of the characters using php substr() but that's not possible in my case. I need to get the excess characters removed by MySQL. How can this be done?
Using the below line
SELECT LEFT(subject , 10) FROM tbl
MySQL Doc.
SELECT SUBSTRING(subject, 1, 10) FROM tbl
Have a look at either Left or Substring if you need to chop it up even more.
Google and the MySQL docs are a good place to start - you'll usually not get such a warm response if you've not even tried to help yourself before asking a question.
I am trying to find a number in a column (a varchar column) using LIKE, I am using a statement like this SELECT * FROM MYTABLE WHERE mynumberslist LIKE '%76%'. The data is stored like this: nums=;76;78;80;81;176; So how do I go about searching for 76 because if I just do LIKE %76% I will get 76 and 176 returned is there anyway around this? I did not design the database, and have no control over the data that is stored, I am only responsible for writing a program that gets the data out.
It is as simple as just
LIKE '%;76;%'
ps: send hello to the database schema author and donate him some money to buy any DB-design-related book
Use regex matching:
SELECT *
FROM MYTABLE
WHERE mynumberslist RLIKE '\b76\b'
This will match even if you don't have a trailing ; at the end - it matched the word 76 (\b means "word boundary" in regex)
SELECT *
FROM MYTABLE
WHERE ';' + mynumberslist + ';' LIKE '%;76;%';