0

我有一个准系统搜索系统,它变得更加复杂,并且出现了许多奇怪的问题。如何重写此搜索查询以更准确和高效

tbl_notes表只有 id,course但为了将其添加到搜索中,我也使用了内部查询来检查它。这有很多问题,我在 mysql 方面不太强,任何帮助将不胜感激。

TBL_注意:

[id][user_id][note_title][note_title][topic][course][note]

TBL_课程:

  [id][user_id][course_name]

   SELECT * FROM `tbl_notes` 
    WHERE `active` = '0' 
    AND `valid_note` = '0' 
    AND `user_id` = '33' 
    AND `note` LIKE '%biology%' 
    OR `topic` LIKE '%biology%' 
    OR `note_title` LIKE '%biology%' 
    OR `course` IN (SELECT `id` FROM tbl_courses WHERE `course_name` LIKE '%biology%' AND user_id = '33') 
    ORDER BY id DESC LIMIT 0, 6

谢谢

4

2 回答 2

2

我会这样写:

SELECT
  DISTINCT n.*
FROM
  `tbl_notes` n left join `tbl_courses` c
  on n.course = c.id
WHERE
  n.`user_id` = '33'
  AND n.`active` = '0' 
  AND n.`valid_note` = '0'
  AND (n.`note` LIKE '%biology%' 
       OR n.`topic` LIKE '%biology%' 
       OR n.`note_title` LIKE '%biology%' 
       OR c.`course_name` LIKE '%biology%') 
ORDER BY n.id DESC LIMIT 0, 6
于 2012-11-14T06:54:37.123 回答
1

也许 ?:

  SELECT * FROM `tbl_notes` 
    WHERE `active` = '0' 
    AND `valid_note` = '0' 
    AND `user_id` = '33' 
    AND 
   (`note` LIKE '%biology%' 
    OR `topic` LIKE '%biology%' 
    OR `note_title` LIKE '%biology%' 
    OR `course` IN (SELECT `id` FROM tbl_courses WHERE `course_name` LIKE '%biology%' AND user_id = '33') 
    )
    ORDER BY id DESC LIMIT 0, 6
于 2012-11-14T06:41:45.387 回答