解密我与mysql语句的匹配

时间:2013-12-17 01:11:06

标签: mysql sql

当我还没有尝试包括得分时,它会起作用。但当然这对于搜索结果的排序很重要。这不起作用:

SELECT * from
          ( SELECT distinct ts.users_user_id from subjects s
          JOIN teachers_subjects ts ON s.subject_ID = ts.subject_id, MATCH  (s.subject_en_name) AGAINST ('math') AS score 
          WHERE MATCH (s.subject_en_name) AGAINST ("math") ) qualified
          JOIN users u ON qualified.users_user_id = u.user_id
          JOIN users_teachers ut ON qualified.users_user_id = ut.user_id
          JOIN teachers_subjects ts2 ON qualified.users_user_id = ts2.users_user_id
          JOIN subjects s2 ON ts2.subject_id = s2.subject_id

这样可行,但不包括每行的分数:

SELECT * from
          ( SELECT distinct ts.users_user_id from subjects s
          JOIN teachers_subjects ts ON s.subject_ID = ts.subject_id 
          WHERE MATCH (s.subject_en_name) AGAINST ("math") ) qualified
          JOIN users u ON qualified.users_user_id = u.user_id
          JOIN users_teachers ut ON qualified.users_user_id = ut.user_id
          JOIN teachers_subjects ts2 ON qualified.users_user_id = ts2.users_user_id
          JOIN subjects s2 ON ts2.subject_id = s2.subject_id

1 个答案:

答案 0 :(得分:0)

问题是score应该是SELECT而不是FROM子句的一部分。试试这种方式

SELECT * 
  FROM
(
  SELECT DISTINCT ts.users_user_id, 
         MATCH (s.subject_en_name) AGAINST ('math') AS score 
    FROM subjects s JOIN teachers_subjects ts 
      ON s.subject_ID = ts.subject_id
   WHERE MATCH (s.subject_en_name) AGAINST ('math')
) q JOIN users u 
    ON q.users_user_id = u.user_id JOIN users_teachers ut 
    ON q.users_user_id = ut.user_id JOIN teachers_subjects ts2 
    ON q.users_user_id = ts2.users_user_id JOIN subjects s2 
    ON ts2.subject_id = s2.subject_id
 ORDER BY q.score DESC