Select from MYSQL where the first four characters are - mysql

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'

Related

Retrieve any name starting with letters 'Ai' in SQL

All I want to do is retrieve the names of the people who start with "ma".
Eg Matt, Mathers, Mac.
First, two letters have to be 'ma'.
Have a look at string comparisons functions.
SELECT *
FROM YOUR_TABLE
WHERE NAME LIKE 'MA%';
SELECT *
FROM YOUR_TABLE
WHERE NAME LIKE 'MA%'
There can be spaces at the beginning of the name just like ' Matthew'. This would fix it:
SELECT *
FROM YOUR_TABLE
WHERE ltrim(NAME) LIKE 'MA%'

How to SELECT w/ LIKE and a '_' delimiter

I have a table with a name field that can have values like:
CHECK_5_20170909
CHECK_1_20170809
CHECK_11_20170809
CHECK_11_20170909
I would now like to query all fields that have a _1_ in the name, but ONLY them.
I tried this: SELECT * FROM tblName WHERE name LIKE '%_1_%';
but that shows me _11_ AND _1_ in my results.
When I try it with CHECKWHATEVER1WHATEVER20170909 and LIKE %WHATEVER1WHATEVER% it works, are there any special rules for _ in a MySQL Query?
Changing it to another delimiter in the MySQL DB would create a hell of work, is there any "workaround"?
You need to add a '\' before each underscore, otherwise its interpreted as a random "wildcard" character.
select * from
(
select 'CHECK_5_20170909' col
union
select 'CHECK_1_20170809'
union
select 'CHECK_11_20170809'
union
select 'CHECK_11_20170909'
) t
where col like '%\_1\_%'
try this using REGEXP
SELECT * FROM tblName WHERE name regexp '_1_';
it will return exact matches record from column for more reference read here

MySQL select based on regular expression

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

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;

MySQL Substring Grab all but last three characters

Basically, I am trying to find the inverse of this command: substring(term, -3).
Try this:
SELECT SubStr(myColumn, 1, LENGTH(myColumn) - 3)
FROM MyTable
or
SELECT LEFT(myColumn, LENGTH(myColumn) - 3)
FROM MyTable
This should do it: SELECT SUBSTRING(term FROM 1 FOR LENGTH(term)-3)
If you need it to match in a where comparison you could use
WHERE somefield LIKE 'term___'