views:

28

answers:

2

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?

A: 
 SELECT * 
 FROM `JULY2010` 
 WHERE `WorkLog`  REGEXP
  (SELECT CONCAT( `UserName`, '|') 
   FROM `OpsAnalyst`)
Michael Pakhantsov
I get this error when performing this query:#1242 - Subquery returns more than 1 row
CapnCrunch
A: 

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

CapnCrunch