我的MySQL查询无效

时间:2010-02-05 18:45:34

标签: php sql mysql

我有以下查询搜索了用户输入数据的剧集指南数据库:

$query = "SELECT * 
            FROM epguide 
           WHERE EpisodeTitle LIKE '%$trimmed%' 
              OR Synopsis LIKE '%$trimmed%' 
              OR Notes LIKE '%$trimmed%' 
        ORDER BY o";

这可行,但是当我添加'Series ='$ ser'AND'时它会停止:

$query = "SELECT * 
            FROM epguide 
           WHERE Series = '$ser' 
             AND EpisodeTitle LIKE '%$trimmed%' 
              OR Synopsis LIKE '%$trimmed%' 
              OR Notes LIKE '%$trimmed%' 
        ORDER BY o";

它看起来应该有效。我该怎么做才能解决它?

2 个答案:

答案 0 :(得分:3)

使用:

  SELECT * 
    FROM epguide 
   WHERE Series = '$ser' 
     AND (EpisodeTitle LIKE '%$trimmed%' 
          OR Synopsis LIKE '%$trimmed%' 
          OR Notes LIKE '%$trimmed%')
ORDER BY o

您需要括号来分隔/分组WHERE子句中的语句。

答案 1 :(得分:2)

你需要决定是否意味着

SELECT * FROM epguide WHERE (Series = '$ser' AND EpisodeTitle LIKE '%$trimmed%') OR Synopsis LIKE '%$trimmed%' OR Notes LIKE '%$trimmed%' ORDER BY o

SELECT * FROM epguide WHERE Series = '$ser' AND (EpisodeTitle LIKE '%$trimmed%' OR Synopsis LIKE '%$trimmed%' OR Notes LIKE '%$trimmed%') ORDER BY o

并适当修改查询。