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%'
Related
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.
I'm storing permissions into DB with Array JSON String, and i want select them by permission specific permission. at this time I'm selecting them like this:
1 | Dog | [3,4]
2 | Cat | [33,4]
3 | Tiger | [5,33,4]
4 | wolf | [3,5]
SELECT * FROM `pages` WHERE access REGEXP '([^"])3([^"])'
it works but not as it should work. This query gives me all records which contains 3 but also it gives which contains 33. my question is how i must format my regexp to get row by specific value into json string.
p.s i have mysql 5.5 so as i know on this version json functions is not supported
If you only have numbers in the fields, you can alter your regexp to only take values where the string you are looking for (here the '3') does not have another number immediately close to it :
SELECT * FROM `pages` WHERE access REGEXP '([^"0-9])3([^"0-9])'
REGEXP '[[:<:]]3[[:>:]]'
That is, use the "word boundary" thingies.
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, '%');
Is there a way I can store multiple values in a single cell instead of different rows, and search for them?
Can I do:
pId | available
1 | US,UK,CA,SE
2 | US,SE
Instead of:
pId | available
1 | US
1 | UK
1 | CA
1 | SE
Then do:
select pId from table where available = 'US'
You can do that, but it makes the query inefficient. You can look for a substring in the field, but that means that the query can't make use of any index, which is a big performance issue when you have many rows in your table.
This is how you would use it in your special case with two character codes:
select pId from table where find_in_set('US', available)
Keeping the values in separate records makes every operation where you use the values, like filtering and joining, more efficient.
you can use the like operator to get the result
Select pid from table where available like '%US%'
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(,.*)?$';