简体   繁体   中英

php query sort the most or largest number of words

Is there any possible way or query for sorting the most word?

for example I have 300 rows and I have this in row 250

Lorem ipsum dolor sit amet, consectetur adipiscing elit

and the row 250 got the most/largest amount of word in the column.

how can I sort that using mysqli_query($dbcon,"SELECT * FROM wordlist") ?

The simplest method is to sort by the length of the column in characters, rather than the number of words:

select wl.*
from wordlist wl
order by char_length(wl.col) desc;

If you actually want words, then perhaps counting spaces is sufficient:

select wl.*
from wordlist wl
order by (length(wl.col) - length(replace(wl.col, ' ', ''))) desc;

Gordon Linoff's answer is correct and I agree with it, however, I describe an alternative solution. You could store the number of words into the wordlist table in a new column . And you could write an insert and update trigger which would do the calculations shown in Gordon Linoff's answer and store the result into the affected record's corresponding field. In that case number of words would be calculated once per record change, or even on specific column change on the record and could read the stored value as many times you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM