开发者

SQL Query for finding a column name where matching text is in column

This is my first stakoverflow question, although I've lurked for quite a while. I'm writing a webapp in PHP/SQLit开发者_如何转开发e, and I'm trying to find a column name along with the following SQL query:

SELECT lexemeid FROM lexeme, sense WHERE lexeme.lexemeid =
sense.senselexemeid AND (lexeme.lexeme LIKE '%apani%' OR lexeme.variant
LIKE '%apani%' OR lexeme.affixedform LIKE '%apani%' OR sense.example
LIKE '%apani%');

Basically, I'm offering a full text lookup for a few different fields. The query above works, but I'd like to get the name of the column where my wildcard matches for each result as well. Basically I want something like the above query with the SELECT looking more like:

SELECT lexemeid, COLUMN NAME FROM...

I'd also welcome any ideas for making my SQL Query look/perform better (maybe using LIKE and IN??). I'm basically trying to join lexeme.lexemeid and sense.senselexemeid and do a wildcard lookup on a text string (in this case, "apani").

Thanks!


Assuming you only have a match in one of the columns, you could use a CASE statement.

SELECT lexemeid,
       CASE WHEN lexeme.lexeme LIKE '%apani%' THEN 'lexeme'
            WHEN lexeme.variant LIKE '%apani%' THEN 'variant'
            ...
            WHEN sense.example LIKE '%apani%' THEN 'example'
       END AS ColumnName
    FROM ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜