I have a column A contains of ID
ex: 1113 992 981 778 718
and my query
select product
from something
where userInvite like '% 1113 %'
It will be fine, but if it on only contains one string
ex: 1113
and query returns there is no results, but if I remove space, it will be fine
E.g.:
select product
from something
where userInvite like '%1113%'
How can I fix it, to search ID in case the column A is not contains a space and contains space.
The best way to do it, would be to use comma as separator, then you could just simply use mysql built in function: find_in_set.
In this case, we can do a little trick:
select product
from something
where find_in_set('123', replace(product, ' ', ',')) > 0
See demo here: http://sqlfiddle.com/#!9/bfcbc/2/0
select product from something where userInvite like '%1113%' should work
Heres a demo
Related
I'm trying to check if a field in a specific table contains also number, in particular I have a record that have the field name which contains this value: Besëlidhja Lezhë vs. Tërbuni Pukë 1 - 1, so I'm trying to get also all the rows of that table that contains a number inside the field name. I tried:
SELECT * FROM `venue` where `name` like '%[0-9]%'
but this will return an empty result, any idea?
This should tell you if name contains any digit (not tested)
SELECT * FROM venue WHERE name REGEXP '[0-9]'
You can try using a Regular Expression that filters for names in your name column with numeric characters . For example:
SELECT * FROM DATA WHERE name REGEXP '[a-z]...[0-9]';
mySQL allows you to use regular expression as a filter !
This should select out for names like Tërbuni Pukë 1 - 1. If you want to practice regular expressions this is a great website to test whether you have the right regex. https://regex101.com/
Hope this helps !
I believe this should work:
SELECT *
FROM venue
WHERE name like '%0%' or name like '%1%' or name like '%2%' or name like '%3%'
and so on til you get to 9. I hope this helps
I have a field called 'areasCovered' in a MySQL database, which contains a string list of postcodes.
There are 2 rows that have similar data e.g:
Row 1: 'B*,PO*,WA*'
Row 2: 'BB*, SO*, DE*'
Note - The strings are not in any particular order and could change depending on the user
Now, if I was to use a query like:
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%'
I'd like it to return JUST Row 1. However, it's returning Row 2 aswell, because of the BB* in the string.
How could I prevent it from doing this?
The key to using like in this case is to include delimiters, so you can look for delimited values:
SELECT *
FROM technicians
WHERE concat(', ', areasCovered, ', ') LIKE '%, B*, %'
In MySQL, you can also use find_in_set(), but the space can cause you problems so you need to get rid of it:
SELECT *
FROM technicians
WHERE find_in_set('B', replace(areasCovered, ', ', ',') > 0
Finally, though, you should not be storing these types of lists as strings. You should be storing them in a separate table, a junction table, with one row per technician and per area covered. That makes these types of queries easier to express and they have better performance.
You are searching wild cards at the start as well as end.
You need only at end.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Reference:
Normally I hate REGEXP. But ho hum:
SELECT * FROM technicians
WHERE concat(",",replace(areasCovered,", ",",")) regexp ',B{1}\\*';
To explain a bit:
Get rid of the pesky space:
select replace("B*,PO*,WA*",", ",",");
Bolt a comma on the front
select concat(",",replace("B*,PO*,WA*",", ",","));
Use a REGEX to match "comma B once followed by an asterix":
select concat(",",replace("B*,PO*,WA*",", ",",")) regexp ',B{1}\\*';
I could not check it on my machine, but it's should work:
SELECT * FROM technicians WHERE areasCovered <> replace(areaCovered,',B*','whatever')
In case the 'B*' does not exist, the areasCovered will be equal to replace(areaCovered,',B*','whatever'), and it will reject that row.
In case the 'B*' exists, the areCovered will NOT be eqaul to replace(areaCovered,',B*','whatever'), and it will accept that row.
You can Do it the way Programming Student suggested
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Or you can also use limit on query
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%' LIMIT 1
%B*% contains % on each side which makes it to return all the rows where value contains B* at any position of the text however your requirement is to find all the rows which contains values starting with B* so following query should do the work.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
I'm trying to create a SQL query which will supply values for auto completion for a text field. Everything is working however I can't seem to create an SQL query which is exact enough for the purposes I want. I am using MySQL.
If there is a space (or multiple spaces) in the search term, I only want the query to do a LIKE comparison on the part of the string after the last space.
For example, say I have two possible values in the database:
Bolt
Bolts Large
Currently if the user types 'Bolt' then a space, both values above are returned using this query -
SELECT name FROM items WHERE name LIKE 'SEARCH_TERM%'
What I want is that if the user types 'Bolt' then a space, then only Bolt is returned from the database.
Effectively meaning that only the last part of the search term after the space is compared using LIKE, the results should match exactly up until the last space.
I've also tried:
SELECT name FROM items WHERE name LIKE 'SEARCH_TERM[a-z]%'
But that actually returns no results using the above scenario.
Is what I'm after possible? I've also tried to explore using Full Text Search but have had no look with that. I believe full text search is enabled on the name field, however I have limited experience with this. The query below didn't work.
SELECT name FROM items WHERE MATCH(name) AGAINST('SEARCH_TERM')
Any advice or points would be very appreciated.
The query
SELECT name FROM items WHERE name LIKE 'Bolt %'
doesn't return any record, because both 'Bolt' and 'Bolts Large' don't match 'Bolt %'.
SELECT name FROM items WHERE name LIKE 'Bolt%'
returns both records, because both 'Bolt' and 'Bolts Large' match 'Bolt%'.
To look for 'Bolt' and not 'Bolts', you must add a space to both your search string and the column string:
SELECT name FROM items WHERE concat(name, ' ') LIKE 'Bolt %'
returns 'Bolt' but not 'Bolts Large'.
SELECT name FROM items WHERE REPLACE(name, ' ', '') LIKE 'SEARCH_TERM%'
You could also use CONCAT and TRIM, or just trim
SELECT name FROM items WHERE name LIKE TRIM('SEARCH_TERM')
or your choice
SELECT name FROM items WHERE name LIKE CONCAT(TRIM('SEARCH_TERM'), '%')
SELECT name FROM items WHERE name LIKE CONCAT('%',TRIM('SEARCH_TERM'))
SELECT name FROM items WHERE name LIKE CONCAT('%',TRIM('SEARCH_TERM'), '%')
I currently have the following code
select *
FROM list
WHERE name LIKE '___'
ORDER BY name;
I am trying to get it so that it only shows names with three or more words.
It only displays names with three characters
I cannot seem to work out the correct syntax for this.
Any help is appreciated
Thankyou
If you assume that there are no double spaces, you can do:
WHERE name like '% % %'
To get names with three or more words.
If you can have double spaces (or other punctuation), then you are likely to want a regular expression. Something like:
WHERE name REGEXP '^[^ ]+[ ]+[^ ]+.*$'
you can count number of words and then select those who are equal or greater then 3 words.
SELECT * FROM list
HAVING LENGTH(name) - LENGTH(REPLACE(name, ' ', ''))+1 >= 3
ORDER BY name
DEMO HERE
*Even if you have multi spaces it will not affect here check this
I do believe what you are after is
SELECT *
FROM list
WHERE name LIKE '% % %'
ORDER BY name;
I would like to replace any 'dot' character in my query string, on SELECTing fields from database.
I'll need to modify lots of queries, I'm willing there's a function that will work to all columns on SELECT. I mean something like this SELECT DOT_TO_COMMA(*) FROM...
Right now what I have:
SELECT price, lastprice FROM products
OUTPUT: 22.10, 5.24
EXPECTATION: 22,10, 5,25
SELECT REPLACE(price, '.', ',') AS price
FROM products;
read more about it here
You have to wrap each column you need to replace with the function. Using replace(*) is not possible.
please try this...
this is working
SELECT REPLACE(price,'.',',') AS price, REPLACE(lastprice,'.',',') AS lastprice FROM products
In my case replace doesn't work well with negative numbers.
I use SELECT FORMAT (price,0).
Second parameter is de decimal numbers
checkout http://www.mysqltutorial.org/mysql-format-function/