Matching Wildcard In Database Table

October 5, 2010

I wanted to do a select that would match specific data in the table, or a wildcard row.

So my database contained

drumcoder
fred
*

If I selected matching on "drumcoder" I wanted the drumcoder row returned.

If I selected matching on "flibble" I wanted the * row returned.

I only wanted one row returned.

You can't do reverse like matching, but what you can do is select by flibble or *, order the results descending, then pick the first row.

SELECT from_email, to_email, subject, body_template 
FROM email_texts
WHERE client_code = ? 
OR client_code = '*' 
ORDER BY client_code desc

Tags: sql wildcard