I have an SQL query which matches results using a LIKE :
_column_name_%
This will return results where the column name is:
_column_name_1
_column_name_2
The end number could be a really high number e.g. 32523, or even 523624366234.
If I use _column_name_%%%%% this would match 32523, but not 523624366234.
How would I get the LIKE to match without typing % repeatedly?
A Simple select query with the LIKE operator should look like this
You have to escape the underscore using "\" if you are having any.
instead of pretext_% use pretext\_%
Select * from mytable where field_1 like 'pretext\_%'
This will return pretext_1 as well as pretext_11
Related
I got this question when I use mysql search something. here is the detailed information.
say I got a table named test with a column named content. in a specific record, the content column holds:
["
/^\w{2,}/","
/^[a-z][a-z0-9]+$/","
/^[a-z0-9]+$/","
/^[a-z]\d+$/"]
there is a linefeed character in the end of the lines(last line excluded)
so when I used the like syntax to search this record, I wrote a SQL like this
select * from test where `content` like
'[\"\n/^\\\\w{2,}/\",\"\n/^[a-z][a-z0-9]+$/\",\"\n/^[a-z0-9]+$/\",\"\n/^[a-z]\\\\d+$/\"]'
and it returned the right result. but when I changed the like to = and this SQL statement didn't work, after I tried several times, I got this SQL statement that worked:
select * from test where `content` =
'[\"\n/^\\w{2,}/\",\"\n/^[a-z][a-z0-9]+$/\",\"\n/^[a-z0-9]+$/\",\"\n/^[a-z]\\d+$/\"]'
it worked. so here is the question:
why on earth the like and = have different escape strategy? in the like statement I have to use \\\\w,\\\\d while in the = statement \\w,\\d just doing fine?
MySQL LIKE operator to select data based on patterns.
The LIKE operator is commonly used to select data based on patterns. Using the LIKE operator in the right way is essential to increase the query performance.
The LIKE operator allows you to select data from a table based on a specified pattern. Therefore, the LIKE operator is often used in the WHERE clause of the SELECT statement.
MySQL provides two wildcard characters for using with the LIKE operator, the percentage % and underscore _.
The percentage (%) wildcard allows you to match any string of zero or more characters.
The underscore (_) wildcard allows you to match any single character.
Comparison operations result in a value of 1 (TRUE), 0 (FALSE), or NULL. These operations work for both numbers and strings. Strings are automatically converted to numbers and numbers to strings as necessary.
The following relational comparison operators can be used to compare not only scalar operands, but row operands:
= > < >= <= <> !=
Note: = is Equal operator and LIKE for Simple pattern matching
I have a query like this:
SELECT * FROM mytable where description like %STRING%
The problem is: When I search for JAVAit returns me even the records with JAVAscript.
But, JAVA != JavaScript, right ? How can I work around it ?
MySQL's LIKE operator isn't really suitable to detect an exact single word inside a string. But REGEXP, which supports regular expressions, can handle this. Consider the following query:
SELECT * FROM mytable WHERE description REGEXP '[[:<:]]Java[[:>:]]';
This corresponds to matching the pattern \bJava\b, i.e. the word Java by itself.
Demo
Edit:
If you are trying to execute this query using Laravel, then whereRaw should come in handy:
$results = DB::table('mytable')
->whereRaw('description REGEXP ?', ['[[:<:]]Java[[:>:]]'])
->get();
So I'm trying to code a PHP script, but we'll just leave it at the SQL part of things since this is where the issue is arising. I've a SELECT * query which should only grab from the rows where the user ID matches, and the badge ID meets their userID followed by an underscore. Although, it's grabbing results that shouldn't be included?
Here's my SQL query:
SELECT *
FROM `user_badges`
WHERE `user_id` = 1
AND `badge_id` LIKE '%1_%'
That should only return badges that start/contain 1_, it is grabbing all the badges that do contain/start with 1_ but it's also grabbing it215. If I search a different user ID, for example my own, it will grab all the badges with 3_ AND it's also grabbing ACH_RoomDecoFurniCount31 which is confusing because it doesn't contain 3_. Maybe there's more to it? Could someone please point me in the right direction.
You need to escape the _ as it's a wildcard character. Your query would should be like this:
SELECT *
FROM `user_badges`
WHERE `user_id` = 1
AND `badge_id` LIKE '%1\_%'
_ is also a wildcard in SQL - A substitute for a single character
_ is also a wildcard character. It means "any single character" (whereas % is "any sequence of characters").
You could escape/quote that _ or use the LOCATE function instead of a pattern match.
WHERE badge_id LIKE '%1\_%'
WHERE locate('1_', badge_id) > 0
_ is a wildcard "_ matches exactly one character." so what you are saying is:
% : starts with anything(or nothing)
1: contains 1
_: has exactly 1 of % (or anything, or nothing)
http://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html
currently I'm making something.
using (var IDatabaseQuery = Lightningbolt.GetDatabaseManager().CreateQueryObject())
{
IDatabaseQuery.SetQuery("SELECT * FROM catalogue_baseitems WHERE name LIKE '%hc2_%' OR name LIKE '%hc3_';");
data = IDatabaseQuery.FetchTable();
}
That's what I use, I want to get all the items beginning with hc2_ and hc3_, however, I only get the items starting with hc2_. In the SQL contains also items starting with hc3_ but it doesn't show while executing the query. What do I wrong?
If you want items beginning with hc2_ or hc3_, you need to make two changes:
Don't use the % at the beginning, and
Escape the underscore because it masks to "any one character"
Try something like this:
SELECT * FROM catalogue_baseitems WHERE name LIKE 'hc2\_%' OR name LIKE 'hc3\_%'
Note that %hc2_% will match any of the following examples:
abchc2X (because of the leading % and the underscore will match the X)
hc23 (because underscore will match the 3)
... and so on
You are missing the second percent sign: '%hc3_%'
You want result which contains hc2_ and hc3_ at the beginning then you need to use clause like this 'hc2_%' or hc3_%.
You are getting results for hc2_ because you are using '%hc2_%', this return any string which contains hc2_ anywhere in the string.
Change your query to this
IDatabaseQuery.SetQuery("SELECT * FROM catalogue_baseitems WHERE name LIKE 'hc2\_%' OR name LIKE 'hc3\_%';");
and don't forgot about underscore(_), this is a wildcard character.
I'm trying to get around pulling all the data from a table, and cycling through it with php. Here's my current Query:
SELECT
*
FROM
ExampleTable
WHERE
StringContains LIKE "%lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh%"
ExampleTable.StringContains has values that look like 'someuser#example.com', 'someuser2#example.com', etc.
This doesn't match because LIKE only finds sub strings of the column value, not the other way around. Any ideas on commands to find rows where the table value is a substring of the passed string?
SELECT
*
FROM
ExampleTable
WHERE
'lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh' LIKE
CONCAT('%', StringContains, '%')
Try this:
SELECT *
FROM ExampleTable
WHERE "lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh" LIKE
CONCAT("%",StringContains,"%")
The key is to recognize that the column variable just represents a string, and the LIKE statement is always comparing two strings in the form
"stringA" LIKE '%stringB%'
Usually people use it to search for a "part" of a string contained in the "whole" database field string, but you can easily switch them. The only extra tool you need is the CONCAT statement, since you want the database field to be the part instead of the whole. The CONCAT statement builds a string with the %'s around the database field string, and the string form of the argument is now equivalent to
"stringB" LIKE "%stringA%"
Just make the LIKE in the opposit order. Since you have to add those % you'll have to concatenate the field first:
SELECT *
FROM ExampleTable
WHERE "lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh" LIKE CONCAT('%', StringContains, '%');