也许我在这里有一个心灵障碍,但我似乎无法让一个AND子句在这个查询中正常工作。我只想返回tbl_mod_Lessons.Approved = 1的结果。
$sql = "SELECT *,
(SELECT AVG(intRating) FROM tbl_mod_LessonReviews LR WHERE LR.intLessonID = LS.intLessonID) rating,
(SELECT strContentArea FROM tbl_mod_ContentAreas CA WHERE CA.intCAID = LS.intContentAreaID) strContentArea
FROM tbl_mod_Lessons LS
WHERE
(LS.strTitle LIKE $search_string) OR
(LS.strStandards LIKE $search_string) OR
(LS.strDescription LIKE $search_string) OR
(LS.strActivities LIKE $search_string) OR
EXISTS(SELECT strContentArea FROM tbl_mod_ContentAreas CA WHERE CA.intCAID = LS.intContentAreaID AND (CA.strContentArea LIKE $search_string))
AND (LS.Approved = 1)
ORDER BY ".$pager->getOrder("LS.intLessonID DESC")."
LIMIT ".$pager->PageLimits();
正如你所看到的,iv试图将其添加进去,这不会破坏查询,但会返回结果,其中Approved = 0。
答案 0 :(得分:1)
尝试下面的一个。我在OR条款周围添加了一些括号。
$sql = "SELECT *,
(SELECT AVG(intRating) FROM tbl_mod_LessonReviews LR WHERE LR.intLessonID = LS.intLessonID) rating,
(SELECT strContentArea FROM tbl_mod_ContentAreas CA WHERE CA.intCAID = LS.intContentAreaID) strContentArea
FROM tbl_mod_Lessons LS
WHERE (
(LS.strTitle LIKE $search_string) OR
(LS.strStandards LIKE $search_string) OR
(LS.strDescription LIKE $search_string) OR
(LS.strActivities LIKE $search_string) OR
EXISTS(SELECT strContentArea FROM tbl_mod_ContentAreas CA WHERE CA.intCAID = LS.intContentAreaID AND (CA.strContentArea LIKE $search_string))
)
AND (LS.Approved = 1)
ORDER BY ".$pager->getOrder("LS.intLessonID DESC")."
LIMIT ".$pager->PageLimits();
答案 1 :(得分:1)
你需要在所有OR条件周围添加这样的括号,以将它们与AND
分开 WHERE
((LS.strTitle LIKE $search_string) OR
(LS.strStandards LIKE $search_string) OR
(LS.strDescription LIKE $search_string) OR
(LS.strActivities LIKE $search_string) OR
EXISTS(SELECT strContentArea FROM tbl_mod_ContentAreas CA
WHERE CA.intCAID = LS.intContentAreaID
AND (CA.strContentArea LIKE $search_string)))
AND (LS.Approved = 1)
答案 2 :(得分:0)
使用or
包围所有其他( )
子句。否则你只是AND
最后一个条款。