English 中文(简体)
MySQL Order by Number of comes
原标题:MySQL Order By Number of Matches

我试图命令MySQL在一行的配对总数中选择发言。 例如,如果表象......

id | dessert | choice
---------------------
1  | pie     | apple pie, chocolate pie 
2  | cake    | chocolate cake, lemon cake, white chocolate cake, triple chocolate cake
3  | donut   | frosted donut, chocolate donut, chocolate cream donut

......以及如果有人查询<代码>chocolate,结果应当命令:

dessert | matches
-----------------
cake    | 3
donut   | 2
pie     | 1

However, I m struggling to make the query ordered like this. I ve looked at other examples here, but they seem overly complicated for what I guessed would be a fairly simple thing.

是否有直截了当的办法来做到这一点? 我对我的SQL说了新话,如果这是一个显而易见的问题,那么我会感到愤怒。

提前感谢你们的想法!

P.S. I m无法在本表中使用fulltext,因此我可以按相关性来决定结果。

最佳回答

How about dividing your data into two tables - one defining the desserts and one defining the choices? This would make the queries simpler and faster.

论文:

id | dessert
------------
1  | pie
2  | cake
3  | donut

选择表:

id | choice
-----------
1  | apple pie
1  | chocolate pie 
2  | chocolate cake
2  | lemon cake
2  | white chocolate cake
2  | triple chocolate cake
3  | frosted donut
3  | chocolate donut
3  | chocolate cream donut

然后,你可以做这样的事情:

select `dessert`, count(*) as `matches`
       from `desserts` join `choices` using (`id`)
       where `choice` like  %chocolate% 
       group by `id`
       order by `matches` desc
问题回答

仅提一字,然后比较长篇的区别

SELECT id
     , desert
     , ( LENGTH(choice) - LENGTH(REPLACE(choice, chocolate ,  )) )
       / LENGTH( chocolate ) AS matches
FROM desert_table
WHERE choice LIKE  %chocolate% 
ORDER BY 3 DESC




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...