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

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

Related

How to search on a varchar column using a number?

I have a varchar(30) column that looks like this:
953-41
975-12
952-13
934-34
All numbers of the column share the structure of: 3 numbers and a dash followed by more numbers.
I want to make a query that works like SELECT * FROM tablename WHERE value = 95341
And get '953-41' only using numbers in the WHERE clause.
I can't change my database and remove the dash, I need to search with a numeric value on rows that mix the numbers I want with a dash in between.
you can try:
MYSQL:
SELECT * FROM tablename WHERE value = INSERT(95341,4,0,'-')
SQL Server:
SELECT * FROM tablename WHERE value = STUFF(95341,4,0,'-')
You can use this:
SELECT *
FROM yourTable
WHERE CAST(REPLACE(colName, '-', '') AS UNSIGNED) = 95341

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: Split string SQL to be used in WHERE clause

I am looking for a way to use the IN keyword in JasperReport. My query looks like:
SELECT * FROM table_name WHERE CID IN (145,45, 452);
following that in jasper report I can setup this;
SELECT * FROM table_name WHERE CID IN ($P{MY_CIDS});
and from my Java I would send $P{MY_CIDS} as a String, so my query will look like
SELECT * FROM table_name WHERE CID IN ("145,45, 452");
My questions is how to transform in SQL "145,45, 452" to a valid query so it would take into consideration each value separately 145, 45, 452
All help is appreciated.
WHERE FIND_IN_SET(CID, "145,45,452")
But this query will always cause table fullscan. So I suggest you to rewrite your code and use proper IN (A, B, C) syntax.
You've two options. The more common, is to rewrite your query like this instead:
SELECT * FROM table_name WHERE CID IN (?, ?, ?, ..., ?);
I'm not sure the other is valid for MySQL, but it works fine in PostgreSQL. It is to use an immutable function that parses the string into a set of integers, and instead use:
SELECT * FROM table_name WHERE CID IN (split_to_int(?));
SELECT * FROM table_name WHERE CID IN ($P!{MY_CIDS});
Just use
$P!{MY_CIDS}
(exclamation mark between $P and {MY_CIDS}).
Jasper will now concatinate the Strings like this:
"SELECT * FROM table_name WHERE CID IN ("
+"145,45,452"
+")";
resulting in:
SELECT * FROM table_name WHERE CID IN (145,45,452);
Took me a long time to get that but now it works for me."

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$'