tags:

views:

113

answers:

1

I have a table with fields id (primary key), name1, name2, and nicknames.

Given a name, I want it to return the entry that contains that name in any of the three fields; however, I want it to return only one entry and sometimes the query that I have returns more than 1 match. Also, if there is more than one match, I want it to return the one that matches name1 first.

This is the query I have now that just gives me everything:

SELECT * FROM table WHERE name1 like "Bob" OR name2 like "Bob" OR nicknames rlike "[,]Bob[,]|[,]Bob$";

Thanks. I am doing this in C++ and mysql++.

+4  A: 
SELECT * FROM (
    SELECT * FROM table WHERE name1 like "Bob" limit 1
    UNION SELECT * FROM table WHERE name2 like "Bob" limit 1
    UNION SELECT * from table WHERE nicknames rlike "[,]Bob[,]|[,]Bob$" limit 1
) AS t1 LIMIT 1;

The limit 1 on each one keeps the database from pulling up 50,000 Bob records just to show you one of them.

Autocracy
thank you very much, this did it
Jack L.