2014-03-07 25 views
0

目前,我有以下記錄:SQL語句來記錄排序取決於具體值

+----+-------------+-----------------+--------+-----------------+-------------+ 
| id | postal_code | program_type_id | gender | school_location | school_type | 
+----+-------------+-----------------+--------+-----------------+-------------+ 
| 1 | 66202  |    2 | female |     |    | 
| 2 | 67487  |    2 | male | rural   | public  | 
| 3 | 68504  |    2 | female | rural   | private  | 
| 4 | 67554  |    2 | female | rural   | public  | 
| 5 | 67212  |    2 | female | urban   | public  | 
+----+-------------+-----------------+--------+-----------------+-------------+ 

我也有如下記載:

mysql> select id, postal_code, program_type_id, gender, school_location, school_type from applications limit 1 offset 6; 
+----+-------------+-----------------+--------+-----------------+-------------+ 
| id | postal_code | program_type_id | gender | school_location | school_type | 
+----+-------------+-----------------+--------+-----------------+-------------+ 
| 7 | 66202  |    2 | female | urban   | public  | 
+----+-------------+-----------------+--------+-----------------+-------------+ 

我會有這個記錄7匹配到以某種方式記錄在數據庫中並給出分數。

評分:

匹配POSTAL_CODE = 1000點 匹配program_type_id = 490點 匹配性別= 20點 匹配school_type = 500點

現在,我要找回應該按以下順序記錄:

+----+-------------+-----------------+--------+-----------------+-------------+ 
| id | postal_code | program_type_id | gender | school_location | school_type | 
+----+-------------+-----------------+--------+-----------------+-------------+ 
| 1 | 66202  |    2 | male |     |    | 1K points 
| 3 | 68504  |    2 | female | rural   | private  | 520 points 
| 2 | 67487  |    1 | male | rural   | public  | 490 points 
| 4 | 67554  |    1 | female | rural   | public  | 20 points 
| 5 | 67212  |    1 | female | urban   | public  | 20 points 
+----+-------------+-----------------+--------+-----------------+-------------+ 
5 rows in set (0.00 sec) 

注意3超2是導致匹配program_type_id和性別會獲得520分,匹配剛剛school_type只會獲得500分。在這種情況下,3分高於2分。

現在,我的問題是,有沒有人有任何想法如何做到這一點,以及如何做到這一點?順便說一句,這是MySQL的5

回答

0

看看case語句中mysql.Your結構將是以下形式

select <fields that matter>, (case when tab1.program_type_id = applications.program_type_id then 490 when 
tab1.postal= applications.postal then 1000 end)from tab1, applications where 
tab1.a = applications.a or tab1.b = applications.b 
當然

,你需要在此查詢更改列的名稱那些你的。

+0

分析你的答案,這可能意味着我們將不得不對每一個組合case語句了。例如,program_type_id和gender將會有自己的case語句。 –

+0

當然是..但不是全部的情況下,但只有當......然後......部分 –

0

我會創造一個功能GET_SCORE(postal_code, table_postal_code, program_type_id, table_program_type_id ...)

在函數內部,你可以比較一下過去的參數和返回計算出的分數。

之後只需在您的SELECT中傳遞所需的參數和表格字段中的功能。

最後只是ORDER BY函數結果列。

UPDATE

DELIMITER $ 

DROP FUNCTION IF EXISTS GET_SCORE$ 

CREATE FUNCTION GET_SCORE(
    postal_code VARCHAR(255) CHARACTER SET utf8, 
    table_postal_code VARCHAR(255) CHARACTER SET utf8, 
    ... all the rest params there) 
    RETURNS INT(11) CHARACTER SET utf8 
READS SQL DATA SQL SECURITY INVOKER 
    BEGIN 
    DECLARE result INT(11); 
calculate all your comparisons and fill the score value to the result 

    RETURN result; 
    END$ 

然後就叫

SELECT GET_SCORE(... pass proper values from necessary tables...) as score 

FROM ... the tables... 
ORDER BY score; 
+0

這是偉大的。你能詳細說明這個功能嗎? –

+0

+1,但我使用了上述方法。 –