简体   繁体   English

根据最佳匹配对MySQL查询结果进行排序(匹配的列数)

[英]Sort MySQL query result according to best match(number of columns match)

How to sort MySQL query result according to number of columns matched using multiple tables.. 如何根据使用多个表匹配的列数对MySQL查询结果进行排序

select tbl_user.*, tbl_user_personal.*, tbl_user_physical.*, tbl_user_education.*, tbl_user_lifestyle.* 
        from
        tbl_user, tbl_user_personal, tbl_user_physical, tbl_user_education, tbl_user_lifestyle 
        where
        tbl_user.user_id = tbl_user_personal.user_id and 
        tbl_user.user_id = tbl_user_physical.user_id and 
        tbl_user.user_id = tbl_user_education.user_id and 
        tbl_user.user_id = tbl_user_lifestyle.user_id and 
        (
            tbl_user_personal.user_community = 'some-text..' or
            tbl_user_personal.user_sub_caste = 'some-text..' or
            tbl_user_personal.user_marital_status = 'some-text..' or
            tbl_user_personal.user_children = 'some-text..' or
            tbl_user.user_age in ('some-text..', 'some-text..') or
            tbl_user.user_country = 'some-text..' or
            tbl_user_physical.user_height in('some-text..', 'some-text..') or
            tbl_user_physical.user_physical_status = 'some-text..' or
            tbl_user_education.user_education_category = 'some-text..' or
            tbl_user_education.user_occupation = 'some-text..' or
            tbl_user_lifestyle.user_eating_habits = 'some-text..'
        )

First, you should learn proper join syntax. 首先,您应该学习正确的join语法。 Simple rule: Never use commas in the from clause. 简单规则:请勿在from子句中使用逗号。

But that is not your question. 但这不是你的问题。 MySQL will treat boolean expressions as numbers in a numeric context, with 1 for true and 0 for false. MySQL将布尔表达式视为数字上下文中的数字,其中1表示true,0表示false。 So, you can just add the values together: 因此,您可以将这些值加在一起:

order by ((tbl_user_personal.user_community = 'some-text..') +
          (tbl_user_personal.user_sub_caste = 'some-text..') +
          (tbl_user_personal.user_marital_status = 'some-text..') +
          (tbl_user_personal.user_children = 'some-text..') +
          (tbl_user.user_age in ('some-text..', 'some-text..')) +
          (tbl_user.user_country = 'some-text..') +
          (tbl_user_physical.user_height in('some-text..', 'some-text..')) +
          (tbl_user_physical.user_physical_status = 'some-text..') +
          (tbl_user_education.user_education_category = 'some-text..') +
          (tbl_user_education.user_occupation = 'some-text..') +
          (tbl_user_lifestyle.user_eating_habits = 'some-text..')
         ) desc

You can also put this expression in the select clause, give it a name, and use that for the order by : 您还可以将此表达式放在select子句中,为其命名,然后按以下order by使用该表达式:

order by NumMatches desc;

Use ORDER BY 使用ORDER BY

ORDER BY table1.column_name ASC|DESC, table2.column_name ASC|DESC; ORDER BY table1.column_name ASC | DESC,table2.column_name ASC | DESC;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM