MySQL - 如何让我的查询执行此操作?

时间:2012-06-02 13:30:55

标签: mysql subquery

我想要做的是通过相似/相似来订购问题然后得到 Result表的结果基于Point表中的点数。

示例:query ='德国位置'

我有以下表格:

表问题

+---------+-----------+---------------+---------------------------------+
| ques_id  | question                                                   |
+---------+-----------+---------------+---------------------------------+
|     1    |  Where is Germany located                                  |
+---------+-----------+---------------+---------------------------------+
|     2    |  where is Germany located on a map                         |
+---------+-----------+---------------+---------------------------------+
|     3    |  where is Germany located in Europe                        |
+---------+-----------+---------------+---------------------------------+

表格结果

+---------+-----------+---------------+---------------------------------+
| resu_id  | result                                                     |
+---------+-----------+---------------+---------------------------------+
|     1    |  Germany is located in Europe                              |
+---------+-----------+---------------+---------------------------------+
|     2    |  Northern hemisphere in Europe                             |
+---------+-----------+---------------+---------------------------------+
|     3    |  between France & Poland                                   |
+---------+-----------+---------------+---------------------------------+
|     4    |  Germany is located in central Europe                      |
+---------+-----------+---------------+---------------------------------+
|     5    |  South of Denmark                                          |
+---------+-----------+---------------+---------------------------------+
|     6    |  52 degrees North, 13 degrees East                         |
+---------+-----------+---------------+---------------------------------+
|     7    |  located on the continent of Europe                        |
+---------+-----------+---------------+---------------------------------+

表格点

+---------+-----------+-----------+-----------+
| pont_id |  ques_id  |  resu_id  |  point    |
+---------+-----------+-----------+-----------+
|    1    |     2     |     6     |    10     |
+---------+-----------+-----------+-----------+
|    2    |     1     |     1     |    8      |
+---------+-----------+-----------+-----------+
|    3    |     2     |     7     |    7      |
+---------+-----------+-----------+-----------+
|    4    |     3     |     5     |    9      |
+---------+-----------+-----------+-----------+
|    5    |     3     |     4     |    8      |
+---------+-----------+-----------+-----------+
|    6    |     1     |     7     |    10     |
+---------+-----------+-----------+-----------+
|    7    |     3     |     2     |    6      |
+---------+-----------+-----------+-----------+
|    8    |     2     |     3     |    4      |
+---------+-----------+-----------+-----------+

我试图

SELECT resu_id FROM `Point` WHERE ques_id is (**?**) ORDER BY `point`

期待结果

+---------+-----------+-----------+--------------------------------------------+
| ques_id |  resu_id  |  point    |    result                                  |
+---------+-----------+-----------+--------------------------------------------+
|    1    |     7     |     10    |    located on the continent of Europe      |
+---------+-----------+-----------+--------------------------------------------+
|    1    |     1     |     8     |    Germany is located in Europe            |
+---------+-----------+-----------+--------------------------------------------+
|    2    |     6     |     10    |    52 degrees North, 13 degrees East       |
+---------+-----------+-----------+--------------------------------------------+
|    2    |     7     |     7     |    located on the continent of Europe      |
+---------+-----------+-----------+--------------------------------------------+
|    2    |     3     |     4     |    between France & Poland                 |
+---------+-----------+-----------+--------------------------------------------+
|    3    |     5     |     9     |    South of Denmark                        |
+---------+-----------+-----------+--------------------------------------------+
|    3    |     4     |     8     |    Germany is located in central Europe    |
+---------+-----------+-----------+--------------------------------------------+
|    3    |     2     |     6     |    Northern hemisphere in Europe           |
+---------+-----------+-----------+--------------------------------------------+

按相关性排序问题,然后根据点值对各个结果进行排序。

感谢大家的帮助,不要对我苛刻:)

2 个答案:

答案 0 :(得分:1)

如果您要查找相关结果数据,可以尝试以下查询:

select 
   p.ques_id, p.resu_id, p.point,
--   q.question,
   r.result -- , p.pont_id
from
   result r
   inner join point p on ( r.resu_id=p.resu_id )
--   inner join question q on ( q.ques_id=p.ques_id and q.ques_id=? ) -- // use this if required
   inner join question q on ( q.ques_id=p.ques_id )
order by 
   q.ques_id, p.point desc, r.result desc
;

上述查询执行的输出:

+---------+---------+-------+--------------------------------------+
| ques_id | resu_id | point | result                               |
+---------+---------+-------+--------------------------------------+
|       1 |       7 |    10 | located on the continent of Europe   |
|       1 |       1 |     8 | Germany is located in Europe         |
|       2 |       6 |    10 | 52 degrees North, 13 degrees East    |
|       2 |       7 |     7 | located on the continent of Europe   |
|       2 |       3 |     4 | between France & Poland              |
|       3 |       5 |     9 | South of Denmark                     |
|       3 |       4 |     8 | Germany is located in central Europe |
|       3 |       2 |     6 | Northern hemisphere in Europe        |
+---------+---------+-------+--------------------------------------+
8 rows in set (0.00 sec)

select中删除您不想再次选择的字段。

答案 1 :(得分:0)

我相信你想要这个::

  SELECT result 
  FROM Result 
  inner join Point on (Result.resul_id=Point.resul_id)
  WHERE Point.ques_id = ? ORDER BY Point.point desc limit 1