Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Zählen der Anzahl der übereinstimmenden Wörter

Ich bin mir nicht sicher, ob es ein besserer Weg ist, aber so würde ich es machen:

SELECT d.id, d.word, LEFT(d.description, 100) description,
  COALESCE(sum(v.vote), 0) votecount,
    (CONCAT(word, description, `usage`) LIKE '%George%')
  + (CONCAT(word, description, `usage`) LIKE '%Tabuki%')
  + (CONCAT(word, description, `usage`) LIKE '%Street%')
  + (CONCAT(word, description, `usage`) LIKE '%Fighter%')
  + (CONCAT(word, description, `usage`) LIKE '%Miley%')
  + (CONCAT(word, description, `usage`) LIKE '%Cyrus%') `match`
FROM definition d
LEFT JOIN vote v ON v.definition_id = d.id
GROUP BY d.id
HAVING `match` > 0
ORDER BY `match` DESC, votecount DESC

Wenn die Strings lang genug sind, kann die wiederholte Verkettung möglicherweise länger dauern als das Erstellen einer abgeleiteten Tabelle (unwahrscheinlich, aber einen Versuch wert):

SELECT id, word, description, votecount,
    (fullDesc LIKE '%George%')
  + (fullDesc LIKE '%Tabuki%')
  + (fullDesc LIKE '%Street%')
  + (fullDesc LIKE '%Fighter%')
  + (fullDesc LIKE '%Miley%')
  + (fullDesc LIKE '%Cyrus%') `match`
FROM (
  SELECT d.id, d.word, LEFT(d.description, 100) description,
    COALESCE(sum(vote), 0) votecount, CONCAT(word, description, `usage`) fullDesc
  FROM definition d
  LEFT JOIN vote v ON v.definition_id = d.id
  GROUP BY d.id
) s
HAVING `match` > 0
ORDER BY `match` DESC, votecount DESC