SQL语句根据特定值对记录进行排名

时间:2014-03-07 04:34:15

标签: mysql sql database algorithm

我目前有以下记录:

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

2 个答案:

答案 0 :(得分:0)

查看mysql中的case语句。您的构造将具有以下形式

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

当然,您需要将此查询中的列名更改为您的列的名称。

答案 1 :(得分:0)

我会创建一个函数GET_SCORE(postal_code, table_postal_code, program_type_id, table_program_type_id ...)

在功能内部,您可以比较过去的参数并返回计算得分。

之后,只需使用SELECT中传递所需参数和表格字段的功能。

最后只是ORDER BY函数结果列。

更新

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;