MySQL select based on regular expression - mysql

In MySQL, I need to select all rows where a field is not an IP address (e.g. 12.32.243.43). Can this be done with MySQL only?
For example: I tried the following but it doesn't match.
select * from mytable where field not like '%.%.%.%'

Sure can. You would be looking for something like:
SELECT * FROM `table` WHERE NOT( some_field REGEXP '^[0-9+]\.[0-9]+\.[0-9+]\.[0-9+]')

If using regular expression is not a requirement, this shall deliver the solution:
select stringBasedIp from x
where inet_aton(stringBasedIp) is null;
select stringBasedIp, (inet_aton(stringBasedIp) is null) as isInvalidIp
from x;
Sample data:
create table x(stringBasedIp varchar(16));
insert into x values
('255.255.255.255'),
('0.0.0.0'),
('0.0.0.300'),
('0.0.0.-1'),
('0.0.0.A'),
('192.168.0.1'),
('400.168.0.1'),
('12.32.243.43'),
('12.32.243.430');
Here are the list of invalid ip:
STRINGBASEDIP
0.0.0.300
0.0.0.-1
0.0.0.A
400.168.0.1
12.32.243.430
Live test: http://www.sqlfiddle.com/#!2/2a4ec/1

Related

select data mysql

i have in my table places named field. there are space separated values(there are problem to store csv value in one field). now i want to fire query like below. how i can do ??
select * from tablename where variablename in places
i did try this way but it shows syntax error.
select * from tablename where variablename in replace(places,' ',',')
### places ###
bank finance point_of_interest establishment
Use FIND_IN_SET
For comma separated
SELECT *
FROM tablename
WHERE ( FIND_IN_SET( 'bank', variablename ) )
Refer : SQL Fiddle
For space separated
SELECT *
FROM tablename
WHERE ( FIND_IN_SET( 'bank', replace(variablename,' ',',') ) )
Refer : SQL Fiddle
The best solution would be to normalise your data structure and do not have a single field storing multiple values.
You can make a query work without normalisation, but any solutions would be lot less optimal from a performance point of view.
Use patter matching with like operator:
... where fieldname like '% searched_value %'
Use the replace() function and combine it with find_in_set():
... where find_in_set('searched_value',replace(fieldname,' ',','))>0
Hi I think your problem comes from the usage of IN
IN for MySql is used like this
SELECT *
FROM table_name
WHERE column_name IN (bank,finance,point_of_interest, establishment);
In case of you want to select places you need to specify each place into value like

Select from MYSQL where the first four characters are

I have a database, in one of the fields occasionally I get an entry which starts off as mail.domainname.com
Is it possible using mysql and php to select only the rows from the field hostname where the first 4 characters = 'mail'?
One way to do it is to use LIKE
SELECT * FROM tablename WHERE hostname LIKE 'mail%'
Another is to use SUBSTR()
SELECT * FROM tablename WHERE SUBSTR(hostname, 1, 4) ='mail'
Case sensitive:
SELECT * FROM tbl WHERE hostname LIKE BINARY 'mail%'
Case insensitive:
SELECT * FROM tbl WHERE hostname LIKE 'mail%'
Yes, you can use SQL LIKE operator: http://www.w3schools.com/sql/sql_like.asp
Simply use:
SELECT * FROM your_table WHERE SUBSTR(field_name, 1, 4) = 'mail'

MySQL select using wildcard (but wildcard in field)

I have a mysql 5 table with a char field holding
DOG
DOUG
CAT
MOUSE
Now I want to SELECT on this field, finding any rows where that field exist within a string, like "DOGGY". (This is opposite of how you normally use a wildcard). So I want a select something like:
SELECT FROM TABLE WHERE FIELD IS SUBSTRING OF "DOGGY"
Is this possible?
select * from mytable where 'doggy' like concat('%',mycol,'%')
You should be able to use LOCATE() to achieve this:
SELECT * FROM MyTable WHERE LOCATE(MyField, 'DOGGY') != 0;

Search MySQL table for a kind of results

How can I search within a MySQL table for results ending in anything except ".jpg"?
Thanks.
You don't need to involve regular expressions, you can just do:
SELECT my_fields
FROM my_table
WHERE my_field NOT LIKE '%.jpg'
SELECT *
FROM mytable
WHERE myfield NOT RLIKE '\\.jpg$'

How do you select from mysql where last character in a string = x?

I'd like to select rows from the database where the last character in the mov_id column equals to 1 and 2.
How would the query look like?
SELECT * FROM `myTable` WHERE `mov_id` LIKE '%1' OR `mov_id` LIKE '%2'
the % character is a wildcard which matches anything (like * in many other places)
If mov_id is a numeric value (TINYINT, INT, etc...) then you should use a numeric operator. For instance, use the modulo operator to keep the last digit
SELECT * FROM mytable
WHERE (mov_id MOD 10) IN (1, 2)
If mov_id is a string, you can use LIKE or SUBSTRING(). SUBSTRING() will be slightly faster.
SELECT * FROM mytable
WHERE SUBSTRING(mov_id, -1) IN ('1', '2')
If your table is big or that query is frequently run, you should definitely consider adding a column to your table, in which you would store mov_id's last digit/character, and index that column.
Try this way too:
SELECT field1
FROM table
WHERE RIGHT(field1, 1) = 'x'
it displays the fields that has last a value of x.
SELECT *
FROM Table
WHERE RIGHT(Column_name, 1) IN ('x')
if you want to match two character just replace 1 by 2.
In general:
RIGHT(COLUMN_NAME, NO_OF_CHARACTER_YOU WANT_TO_MATCH_FROM_LAST)
And if you want to match the starting char just use LEFT instead of RIGHT
You could also do something like:
select
your, fields, go, here
from table
where
substring(mov_id, (char_length(move_id) - 1)) = x
SELECT * FROM table WHERE mov_id REGEXP '1$' OR mov_id REGEXP '2$'