MySQL query results used in LIKE - mysql

I am trying to write a query to pull all the rows that contain a username from a large list of usernames in a field.
For example, the table contains a column called 'Worklog' which contains comments made by users and their username. I need to search that field for all user names that are contained in a list I have.
I have tried a few different things but can't get anything to work. So far, this is kind of what I have tried:
SELECT *
FROM `JULY2010`
WHERE `WorkLog`
IN (
SELECT CONCAT( '%', `UserName` , '%' )
FROM `OpsAnalyst`
)
The problem is I need to use LIKE because it is searching a large amount of text, but I also have a large list that it is pulling from, and that list needs to be dynamic because the people that work here are changing frequently. Any ideas?

SELECT *
FROM `JULY2010`
WHERE `WorkLog` REGEXP
(SELECT CONCAT( `UserName`, '|')
FROM `OpsAnalyst`)

I slightly modified this and used GROUP_CONCAT() and now my query looks like this:
SELECT *
FROM JULY2010
WHERE `WorkLog`
REGEXP (
SELECT GROUP_CONCAT(`UserName` SEPARATOR '|') FROM `OpsAnalyst`
)
I am now getting a result set, but it seems like it isn't as many results as I should be getting. I'm going to have to look into it a little more to figure out what the problem is

Related

Replace text in SQL

I have a field called EMAIL_ADDRESS. One of the records would be:
john#gmail.com, mike#gmail.com, joe#yahoo.com, george#yahoo.com, fred#gmail.com
I wan to remove all yahoo addresses in my SELECT query to get:
john#gmail.com, mike#gmail.com, fred#gmail.com
If I use
REPLACE(SM.SCORECARD_EMAIL_ADDRESS, 'joe#yahoo.com,', '')
this works.
If I want to remove ALL yahoo email addresses this doesn't work:
REPLACE(SM.SCORECARD_EMAIL_ADDRESS, '%#yahoo.com,', '')
because wildcards don't seem to work as it's looking for % in the string.
You should probably fix your table design and stop storing CSV lists of email addresses. Instead, get each email onto a separate record. As a short term fix, if you're running MySQL 8+, you may use REGEXP_REPLACE():
UPDATE yourTable
SET EMAIL_ADDRESS = REGEXP_REPLACE(
REGEXP_REPLACE(EMAIL_ADDRESS, '(, )?\\S+#yahoo\\.com,?', ','), '^,+|,+$', '')
WHERE EMAIL_ADDRESS LIKE '%#yahoo.com%';
If you don't need to udpate records but you want them only in the SELECT query you can use NOT LIKE operator
SELECT * FROM your_table WHERE email NOT LIKE '%yahoo.com'
So you get records that doesn’t match the like pattern

Query MySQL field for LIKE string

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*%'

MySQL - How can I perform this string search?

I have a table which features 37 columns, of various types, INTs and VARCHARS and 2 LONGTEXT columns. The client wants a search to search the table and find the rows that match.
However, I'm having trouble with this. Here is what I've done so far:
1) My initial view was to do a massive set of OR queries - however I was put off this by the fact I would need to supply the search data ~30 times, which is massive repetition and I'm sure there;s a better way than this.
Code:
SELECT
MemberId,
MemNameTitle,
MemSName,
MemFName,
MemPostcode,
MemEmail
FROM
MemberData
WHERE
MemFName LIKE CONCAT('%',?,'%') OR
MemSName LIKE CONCAT('%',?,'%') OR
MemAddr LIKE CONCAT('%',?,'%') OR
MemPostcode LIKE CONCAT('%',?,'%') OR
MemEmail LIKE CONCAT('%',?,'%')
...etc...
Etc. Etc. That's a massive set of OR's and really unwieldy.
2) I thought I'd try and rework it to place all the columns in brackets and then only ask the query once, I saw a similar piece of code on SO but not sure that was correctly working, but it was an insprition, at least:
SELECT
MemberId,
MemNameTitle,
MemSName,
MemFName,
MemPostcode,
MemEmail
FROM
MemberData
WHERE
(MemNameTitle OR
MemFName OR
MemSName OR
MemAddr OR
MemPostcode OR
MemEmail OR
MemSkype OR
MemLinkedIn OR
MemFacebook OR
MemEmailTwo ...etc...) LIKE CONCAT('%',?,'%')
GROUP BY
MemberId
This code executes without apparent error but fails as it always returns no result, as in 0 fields returned. I can't see why, from an initial view,
3) So, with some research on OS I found a rearrangement using the IN keyword, but from previous questions on here Is it possible to use LIKE and IN for a WHERE statment? it appeared not to work.
What I wanted to get was something like:
SELECT
MemberId,
MemNameTitle,
MemSName,
MemFName,
MemPostcode,
MemEmail
FROM
MemberData
WHERE
MemNameTitle,
MemFName,
MemSName,
MemAddr,
MemPostcode,
MemEmail,
MemSkype,
MemLinkedIn,
...etc ...
MemFax,
MemberStatus,
CommitteeNotes,
SecondAddr,
SecondAddrPostcode IN (LIKE CONCAT('%',?,'%') )
This is crudy syntax but I hope you get the idea I want to get, I want to search many fields for the same value using a LIKE % % clause. Fields are variously TEXT/VARCHAR types.
4) I then looked into MySQL full text searches but this quickly became useless as this is only applied to TEXT type rather than VARCHAR type searching. I considered before each search changing each VARCHAR column to a TEXT column but figured that was also be relatively processor intensive and seemed illogical for a search that many people must want to do?
So, I'm out of ideas..... Can you help me search this way? Or suggest why my code in attempt 2 always returns Zero rows?
Cheers
Additional Work:
5) I have been looking at rearranging the IN clause statement and came up with this:
SELECT *(lazy typing!) WHERE
CONCAT('%',?,'%') IN
(MemNameTitle,
MemFName,
MemSName,
MemAddr,
MemPostcode,
MemEmail,
MemSkype,
...etc...
CommitteeNotes,
SecondAddr,
SecondAddrPostcode)
GROUP BY MemberId
However this returns a result, but the result is always the last row of the table. This doesn't work.
Solution 1:
From Ravinder, using CONCAT_WS for all the fields - this works in my case, although something in my mind does find CONCATs somewhat ugly, but oh well.
SELECT * FROM MemberData WHERE
CONCAT_WS('<*!*>',
MemNameTitle, MemFName, MemSName,
MemAddr, MemPostcode, MemEmail,
MemSkype, MemLinkedIn,
...etc...
MemberStatus, CommitteeNotes, SecondAddr,
SecondAddrPostcode)
LIKE CONCAT('%',?,'%')
GROUP BY MemberId ";
The table will eventually have a few thousand rows, and I am a little worried that as this query will concat 24 columns for each row on the table for each search, that this could easily become quite expensive and inefficient (ie slow), so if anyone has any ways of either
i) searching without CONCAT columns or
ii) making this solution faster/ more efficient
please share!!
There is a workaround solution. But I feel this is too crude and performance may not be that good.
where
concat_ws( "<*!*>", col1, col2, col3, ... ) like concat( '%', ?, '%' )
Here, I used '<*!*>' just as an example separator.
You have to use a pattern string as separator which, you are sure that,
is not part of the place holder value or
is not part of the generated string when 2 or more columns are
concatenated
Refer to Documentation:
MySQL: CONCAT_WS(separator,str1,str2,...)
It won't skip empty column values but NULLs.
One rather ugly way to do it would be
SELECT
MemberId,
MemNameTitle,
MemSName,
MemFName,
MemPostcode,
MemEmail
FROM
MemberData
WHERE
CONCAT(
MemNameTitle,
MemFName,
MemSName,
MemAddr,
MemPostcode,
MemEmail,
MemSkype,
MemLinkedIn,
...etc ...
MemFax,
MemberStatus,
CommitteeNotes,
SecondAddr,
SecondAddrPostcode) LIKE CONCAT('%',?,'%')
so you first concatenate all the columns you want to search and then look in the resulting big string for your text.
But i guess you can see that this is far from performant and optimal. But since you are using the % sign in the beginning and end of your searches, you couldn't use any indexes anyway.
Warning:
Be aware that this CONCAT may fail in case one of your columns contains a null value, because then the whole CONCAT will return null!

SQL Text Search - EXACT before space, LIKE after in search term

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'), '%')

Ordering mysql result by number of regexp matches

I've the following query. It selects all posts where the title contains the words green, blue or red.
SELECT id, title FROM post WHERE title REGEXP '(green|blue|red)'
I would like to sort the results in such a way that the title with the most matches (all three words) and thus the most relevant one, is listed first. Is this possible in this scenario and if so, how I would go on about it?
Thanks
You must split the regex. Either to different conditions or different queries:
SELECT COUNT(results.username) as count, results.* FROM (
SELECT * FROM `post` WHERE `title` LIKE "%blue%"
UNION SELECT * FROM `post` WHERE `title` LIKE "%red%"
UNION SELECT * FROM `post` WHERE `title` LIKE "%green%"
) as results GROUP BY results.title ORDER BY count DESC;
Note: I used LIKE instead of REGEXP, becouse when you split the condition you wont need it anymore according to your example. LIKE is a bit faster then regex, but if your pattern is more complex, then you can always replace it back.