mysql cuenta palabra en sintaxis sql [duplicado]

Lets have a simple query:

SELECT myfield
FROM   mytable
WHERE  criteria

The above query gives us 10 rows of results for example. The field myField is a text field.

My question is this: is it possible to alter the above query and get the total word count of the myField ¿campo?

Editado: By total word count I mean the total word count of all selected fields in query, in all rows

Ejemplo

+------------------------------+-------+
| sentence                     | Words |
+------------------------------+-------+
| Hello World                  |     2 |
| Hello World                  |     2 |
| Mary had a little lamb       |     5 |
| Her fleece was white as snow |     6 |
| Everywhere that mary went    |     4 |
| Umm, sheep followed her      |     4 |
+------------------------------+-------+

preguntado el 28 de agosto de 12 a las 11:08

The question is very confusing. Please provide us with a small data example and the expected result. -

So far - 3 different interpretations. I rest my case. -

@Andrius Naruševičius why is it confusing? I ask for a way to get total word count of a column. Where is the confusion? -

@andrew Could you please elaborate your question to remove the confusion..? What actually you want the total no of words in each cell of myfield or the total no of cells under myfeild fulfilling the criteria.? -

@andrew Please take a look at the accepted answer. Then compare the visual example, which is really easily understandable to the poorly worded sentence in the question (no offense). Also, due to the fact my comment got thumbed up several times, I would say it wasn't me who thought this way. Always try to add an example when possible to get rid of any potential misinterpretations. :) -

1 Respuestas

Use the excellent function from esta pregunta by @otis in your query:

mysql> select * from test;
+----+------------------------------+
| id | sentence                     |
+----+------------------------------+
|  0 | Hello World                  |
|  1 | Hello World                  |
|  2 | Mary had a little lamb       |
|  3 | Her fleece was white as snow |
|  4 | Everywhere that mary went    |
|  5 | Umm, sheep followed her      |
+----+------------------------------+
6 rows in set (0.00 sec)

mysql> SELECT sentence, wordcount(sentence) as "Words" from test;
+------------------------------+-------+
| sentence                     | Words |
+------------------------------+-------+
| Hello World                  |     2 |
| Hello World                  |     2 |
| Mary had a little lamb       |     5 |
| Her fleece was white as snow |     6 |
| Everywhere that mary went    |     4 |
| Umm, sheep followed her      |     4 |
+------------------------------+-------+
6 rows in set (0.02 sec)

To make the function work, you need to execute the declaration of the function in MySQL. It is just like executing any other query:

mysql> DELIMITER $$
mysql> CREATE FUNCTION wordcount(str TEXT)
            RETURNS INT
            DETERMINISTIC
            SQL SECURITY INVOKER
            NO SQL
       BEGIN
         DECLARE wordCnt, idx, maxIdx INT DEFAULT 0;
         DECLARE currChar, prevChar BOOL DEFAULT 0;
         SET maxIdx=char_length(str);
         WHILE idx < maxIdx DO
             SET currChar=SUBSTRING(str, idx, 1) RLIKE '[[:alnum:]]';
             IF NOT prevChar AND currChar THEN
                 SET wordCnt=wordCnt+1;
             END IF;
             SET prevChar=currChar;
             SET idx=idx+1;
         END WHILE;
         RETURN wordCnt;
       END
     $$
Query OK, 0 rows affected (0.10 sec)

mysql> DELIMITER ;

contestado el 23 de mayo de 17 a las 13:05

Entonces es un duplicado de stackoverflow.com/questions/748276/… - Adi

Yes, but perhaps OP doesn't know how to apply the function to their query. - burhan khalid

Given that you have edit privileges, maybe you could've edited that answer to provide a usage example then linked to it in a comment. Maybe. - Adi

Do you mean other than the example posted? - burhan khalid

@BurhanKhalid There was no example in the question itself. There was a query which did not help at all, because this could have been interpreted in at least 2 ways. Since you can edit everything what is valuable in the question, I suggest you moving your own example to the question. Otherwise, your answer could be a good wiki for all of us and we appreciate that, but it does not answer the question, because the question is poorly worded. - Andrius Naruševičius

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.