1

I am trying to search inside a sentence using the string from another column in SQL. I have a large list of keywords and their rating on a scale from 1 to 6. I am trying to search through data pulled down from Twitter to see what people thought of certain movies (i.e. Wreck It Ralph) and give the movie an aggregate score. The problem I am having is that the data pulled from Twitter isn't broken up, but still in a full sentence. I can't figure out a way to search through the sentence for the keyword. Here is a query that works:

SELECT SUM(K.Score) 
FROM wreck_it_ralph WIR, Keyword K
WHERE t_text LIKE '%fantastic%'
       AND K.word = 'fantastic';

Here is a query I tried using concatenation, but was unsuccessful:

SELECT SUM(K.Score) 
FROM Wreck_it_Ralph WIR, Keyword k
WHERE t_text LIKE ('%' + k.word + '%');
9
  • SELECT SUM(K.Score) FROM Wreck_it_Ralph WIR, Keyword k WHERE t_text LIKE '%' + k.word + '%'; Commented Apr 24, 2013 at 0:46
  • The concatenated query seems like it would work. What database server are you using? Commented Apr 24, 2013 at 0:48
  • It's one on campus. Oracle, I believe Commented Apr 24, 2013 at 0:50
  • try query in my comment Commented Apr 24, 2013 at 0:51
  • I fixed the query so it's WHERE WIR.t_text LIKE ('%' + k.word + '%') but now I get back an error that says "invalid number" Commented Apr 24, 2013 at 0:52

2 Answers 2

1

Different databases have different ways of concatenating strings. In Oracle or Postgres, try this:

SELECT SUM(K.Score) 
FROM Wreck_it_Ralph WIR, Keyword k
WHERE t_text LIKE '%' || k.word || '%'

In SQL Server or Sybase:

SELECT SUM(K.Score) 
FROM Wreck_it_Ralph WIR, Keyword k
WHERE t_text LIKE '%' + k.word + '%'

And in MySQL:

SELECT SUM(K.Score) 
FROM Wreck_it_Ralph WIR, Keyword k
WHERE t_text LIKE concat('%', k.word, '%')

Do note that you have a problem with partial word matches. So, something "dislike" will match "like". You may want to look into the full text capabilities of whichever database you are using, because that takes into account punctuation, full words, and stop-lists.

Sign up to request clarification or add additional context in comments.

3 Comments

this query worked: SELECT SUM(K.Score) FROM Wreck_it_Ralph WIR, Keyword k WHERE t_text LIKE '%' || k.word || '%'
THANK YOU! I don't understand what these || symbols are doing though
@user2313460 . . . That happens to be the Oracle syntax for string concatenation.
0

MSSQL-:

SELECT SUM(K.Score) 
FROM Wreck_it_Ralph WIR, Keyword k
WHERE t_text LIKE '%' + k.word + '%';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.