I have a varchar column in a table and I need to find all values in the column that matches a pattern. The pattern is either parta-partb-partc or parta-partb-positiveInteger-partc. Except for the number part, everything is fixed.
For example
**someColumn**
parta-partb-partc
parta-partb-1-partc
parta-partb-1xyz-partc
parta-partb-123-partc
parta-partb-abc-partc
My search query should return
parta-partb-partc
parta-partb-1-partc
parta-partb-123-partc
So far this is what I got
SELECT *
FROM tableName
WHERE
someColumn ='parta-partb-partc'
OR someColumn LIKE 'parta-partb-%[0-9]-partc'
I am not able to construct the LIKE part to get only strings with positive number in between.
For MySQL following query gives expected result
SELECT *
FROM
tableName
WHERE
someColumn = 'parta-partb-partc'
OR
someColumn REGEXP 'parta-partb-[0-9]+-partc'
;
For Oracle following query gives expected result
SELECT *
FROM tableName
WHERE
someColumn = 'parta-partb-partc'
OR
REGEXP_LIKE(someColumn, 'parta-partb-[[:digit:]]+-partc')
;
For MS-SQL unfortunately REGEX is not supported in where clause, so you have to do something like following. [Specific to your example data]
SELECT *
FROM tableName
WHERE
someColumn = 'parta-partb-partc'
OR
( someColumn LIKE 'parta-partb-[0-9]-partc'
OR someColumn LIKE 'parta-partb-[0-9]%[0-9]-partc'
AND someColumn NOT LIKE 'parta-partb-[0-9]%[a-zA-Z]%[0-9]-partc'
AND someColumn NOT LIKE 'parta-partb-[0-9]%[a-zA-Z]-partc'
)
Related
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
When I run a query like this:
SELECT * FROM table WHERE columnName = AES_ENCRYPT('value','SecretKey')
I'm returned an empty set, even though there are rows in the db that match the search query.
What would the correct syntax for something like this look like?
SELECT * FROM table WHERE columnName = AES_ENCRYPT(columnName,'SecretKey')='value'
Column1 has a record with the value example.
A user submits the value somethingwithexample.
The end of his string matches the value in Column1. How to SELECT Column1 in MySQL?
You could use the like operator:
SELECT *
FROM mytable
WHERE :userinput LIKE CONCAT('%', mycolumn)
EDIT:
As noted in the comment, if the column is empty, this will evaluate as userinput LIKE '%', which is by definition always true. This edge-case should be handled explicitly:
SELECT *
FROM mytable
WHERE mycolumn IS NOT NULL AND
mycolumn != '' AND
:userinput LIKE CONCAT('%', mycolumn)
select * from table_name
where column_name like '%example'
select * from your_table
where $userInput like concat('%', col)
select * from tablename where value like '%example';
will select all rows whose value column has values that are ending with example
for example: mathexample, scienceexample;
select * from tablename where value like '%example%';
will select all rows whose value column has values such as mathexample1, scienceexample2 etc
I have the following problem with a MySQL query in C#:
Given a list of strings, I want to query the database for any rows that match said strings. The strings are unique in that each string matches no more than one row. Today, my query looks something like this:
SELECT Id FROM SomeTable
WHERE SomeColumn IN("foo", "bar", "baz")
Now, ideally I would like to be able to map the result from the query directly to the list of strings I supplied in the IN clause:
String Returned ID
------------------------------------------
foo 123
bar NULL <-- Missing row filled with NULL
baz 42
This works fine as long as all strings I pass to the query match a row. When one is missing, however, I would like to fill in the blank with a NULL as in the example above.
Is there any way to accomplish this?
Edit: I should probably have pointed out that the solution must scale to a lot of strings. The way I do it right now is that I pass 100 at a time through the IN clause.
You could do this:
SELECT
helper.SomeColumn,
SomeTable.Id
FROM
(
SELECT 'foo' AS SomeColumn
UNION SELECT 'bar'
UNION SELECT 'baz'
) AS helper
LEFT JOIN SomeTable ON SomeTable.SomeColumn = helper.SomeColumn
Of course you can create the helper table (as a temp table) beforehand instead of inline.
Anyway, maybe it is smarter and more efficient to just do the query you have (WHERE SomeColumn IN (...)) and simply figure out the missing rows in your application. You will loop over them anyway, so you will notice.
What you could do is SELECT the set of strings as a result set and then LEFT JOIN on SomeTable.SomeColumn.
Try this:
SELECT Id
FROM (
SELECT "foo" SomeColumn
UNION ALL
SELECT "bar" AS SomeColumn
UNION ALL
SELECT "baz" AS SomeColumn
) b
LEFT JOIN
SomeTable a
ON a.SomeColumn = b.SomeColumn
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$'