I have a table that has a comma separated list of ids in one of the fields (not my doing). I want to know if I can use LIKE to match a number in this string? The problem is I don't want to get similar numbers. Is there a way to match a number with no numeric charcters on either side?
SELECT * FROM table WHERE activitiesids LIKE 6
| activitiesids |
---+---------------+---
| 3,16,8,6 |
---+---------------+---
| 6 |
---+---------------+---
| 7,560 |
---+---------------+---
Haven't tested but you can try something like this:
SELECT * FROM table WHERE activitiesids REGEXP '[[:<:]][0-9]+[[:>:]]';
Something like that:
WHERE ids LIKE '%,16,%' OR ids LIKE '%,16' OR ids LIKE '16,%';
Postgresql even has pattern matching - I don't know for mysql:
WHERE ids ~ '^(.*,)?16(,.*)?$';
Related
I have a MySQL database with a varchar column (although the column type can be changed if needed).
The column stores some ids separated with underscores like so:
Row 1: 1
Row 2: 1_2_3
Row 3: 10_2
Row 4: 4_5_1
Is there anyway in this structure to query that column for 1 and return all rows with 1 (but not Row 3 which contains 1 but the ID is 10).
To get the current results I am attempting to search the column LIKE %1%.
Or do I need to change the structure to achieve the result I want?
Maybe you can try:
select *
from t
where c like '1\_%'
or c like '%\_1'
or c like '%\_1\_%'
or c = '1'
You need to escape the underscore as \_, since SQL defines it as a wildcard and will match any character.
If we had a comma separator, then we could use MySQL FIND_IN_SET function.
We can use MySQL REPLACE function to change the underscores to commas,
e.g.
SELECT t.*
FROM t
WHERE FIND_IN_SET('1',REPLACE( t.id ,'_',','))
Reference:
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_find-in-set
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_replace
NOTE:
Storing underscore separated lists is an antipattern. See Chapter 2 of Bill Karwin's book "SQL Antipatterns: Avoiding the Pitfalls of Database Programming"
https://www.amazon.com/SQL-Antipatterns-Programming-Pragmatic-Programmers/dp/1934356557
With the operator like:
select * from tablename
where concat('_', id, '_') like '%#_1#_%' escape '#'
See the demo.
Results:
| id |
| ----- |
| 1 |
| 1_2_3 |
| 4_5_1 |
I have table region with fields
id | name | dates
Sample data
1 | local | "2018-01-01", "2018-01-02", "2018-01-03"
I want a query like this
SELECT * FROM region WHERE "2018-01-02" in (region.dates)
but this does not work. I do not use json data in this case. How can I change it?
(My)SQL doesn't work like that as it will see region.dates as a simple string. In which case, you can do SELECT * FROM region WHERE dates LIKE '%"2018-01-02"%';
However, a better solution would be to devolve that column into another table.
Im going to make a search field, where the user types the item he/she is looking for.
lets say helm in this example.
The database holds items, like:
id | name |
1 | helm of c
2 | helm of 9
3 | helm
4 | helmset
5 | helmapo
6 | haloween
how can i make the mysql query in this case return row 1-5, cause its all match?
You can use wildcards
SELECT * FROM table WHERE name LIKE '%helm%'
More information on wildcards: http://www.w3schools.com/sql/sql_wildcards.asp
You can try to make wildcard search like this:
select * from tablename where name like 'helm%'
Also if you the word helm can appear in the middle of the column value or any place then put it like
select * from tablename where name like '%helm%'
You can use sql wildcards to handle this use case
SELECT row FROM tablename WHERE field LIKE '%helm%'
If you encapsulate the search term with % on both sides the SQL will return any row containing the search term at any index position, however if you only want return rows that start with helm then you would use the term
'helm%'
I'm trying create a MySQL query which will return a row (or more, but I'm only interested in a single row) an area suggestion when provided with a postcode.
So, I have a table like so
area_id | postcode_start
1 | NE
2 | DL
3 | DL1
4 | DL2
5 | DL6
...
And I wish to provide a postcode, for example DL5 8TB and for it to return the row 1 | DL as the first characters match.
Another example DL6 4GH would return two rows, 5 | DL6 and 2 | DL
SELECT *
FROM (`area_postcodes`)
WHERE CONCAT(LOWER(`area_postcodes`.`postcode_start`),'%')
LIKE 'dl5 8tb';
Apparently it's a valid query but it always returns an empty set.
You'll see I've added the wildcard % to the other side of the query but it doesn't seem to work and I don't know where else to even consider guessing how to go about it, it's almost like the reverse of a normal MySQL LIKE query.
You were very close:
SELECT
*
FROM
area_postcodes
WHERE
'dl5 8tb' LIKE CONCAT(postcode_start, '%');
I have a bunch of regular expressions in a MySQL table. I want to know whether a given string matches a part of any regular expression or not.
Eg:
+----+--------------------------------+
| id | regexps |
+----+--------------------------------+
| 1 | foo-[0-9]*\.example\.com |
| 2 | (bar|tux)-[0-9]*\.example\.com |
+----+--------------------------------+
(The regexps attribute is of VARCHAR type)
foo-11.example.com matches the first regexp.
I want a MySQL query that returns the first row with the given string as foo-11
This should do it on MySql:
select * from table t where 'foo-11.example.com' rlike t.data;
There are other ways in PostGreSQL. Here's the link from where I have referenced this:
http://www.tutorialspoint.com/mysql/mysql-regexps.htm
Match a Query to a Regular Expression in SQL?
PS: Using * is tricky though!