Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 根据特定值对记录进行排序的SQL语句_Mysql_Sql_Database_Algorithm - Fatal编程技术网

Mysql 根据特定值对记录进行排序的SQL语句

Mysql 根据特定值对记录进行排序的SQL语句,mysql,sql,database,algorithm,Mysql,Sql,Database,Algorithm,我目前有以下记录: +----+-------------+-----------------+--------+-----------------+-------------+ | id | postal_code | program_type_id | gender | school_location | school_type | +----+-------------+-----------------+--------+-----------------+-------------+

我目前有以下记录:

+----+-------------+-----------------+--------+-----------------+-------------+
| 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
与数据库中的记录进行匹配,并给出分数

评分:

匹配邮政编码=
1000
点数 匹配程序类型id=
490
点 匹配性别=
20分
匹配
学校类型
=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,因为匹配课程类型、id和性别将获得520分,而匹配学校类型仅获得500分。在这种情况下,3分高于2分


现在,我的问题是,有没有人知道怎么做,怎么做?顺便说一句,这是MySQL 5。

看看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
select,(tab1.program\u type\u id=applications.program\u type\u id时的情况),然后在
tab1.postal=applications.postal然后是1000 end)从tab1开始,应用程序在
tab1.a=应用程序.a或tab1.b=应用程序.b

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

我将创建一个函数
GET\u SCORE(邮政编码、表格邮政编码、程序类型id、表格程序类型id…

在函数中,您可以比较过去的参数并返回计算的分数

之后,只需使用
中的函数,选择
传递所需参数和表格字段

最后,只需按
排序函数结果列

更新

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;

分析您的答案,这可能意味着我们也必须为每个组合提供一个案例陈述。例如,程序类型id和性别会有自己的case语句,这很好。你能详细说明一下这个功能吗?