如何根据使用多个表匹配的列数对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..'
)
答案 0 :(得分:1)
首先,您应该学习正确的join
语法。简单规则:切勿在{{1}}子句中使用逗号。
但这不是你的问题。 MySQL将布尔表达式视为数字上下文中的数字,其中1表示true,0表示false。因此,您可以将值一起添加:
from
您也可以将此表达式放在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
子句中,为其命名,并将其用于select
:
order by
答案 1 :(得分:0)
使用ORDER BY
ORDER BY table1.column_name ASC | DESC,table2.column_name ASC | DESC;