2012-06-02 48 views
2

我想要做的是通過相似/相似的順序問題,然後從Result表中得到 結果來自Point表中的點數。MySQL - 我如何讓我的查詢做到這一點?

:查詢= '德國位置'

我有以下表:

表問題

+---------+-----------+---------------+---------------------------------+ 
| 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   | 
+---------+-----------+-----------+--------------------------------------------+ 

排序問題,那麼基於點值相應的結果。

感謝所有的幫助,不要對我:)

+0

如果您嘗試過,結果出了什麼問題?您是否正在按特定順序查找正確答案的結果ID或問題ID的結果ID列表? –

+0

@Ravinder首先,我的查詢不排序問題的相關性,那麼它應該從'點'表找到resu_id然後按點排序它們。 – bekman

+0

查看我更新的查詢,並且如果需要更多內容,則返回beck ... –

回答

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 
+0

這隻返回帶有最高點結果id的單行,這可能表示問題ID的正確答案。 –

+0

那就是我的查詢 –

+0

OP是否在尋找結果的第一條記錄? –

1

如果您正在尋找相關的結果數據,你可以嘗試以下方法查詢:

+---------+---------+-------+--------------------------------------+ 
| 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 
    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 
; 

上述查詢執行的輸出

select中刪除字段,無論您不想再次選擇。

+0

謝謝你,但它應該返回最高點的結果。我在上面的問題中添加了「期待結果」。 – bekman

+0

@bekman解決方案修改爲按訂單並選擇字段 –

+0

謝謝,你是一個生命的救星。但在哪個部分你排序的問題? – bekman