2011-04-25 66 views
1

我试图从基于heightwidth列的多个组合的表中选择数据。但是,我正在使用的查询不起作用。从多个列组合中选择表中的数据

$sql_wallpaper = " 
    SELECT * 
    FROM wallpaper 
    WHERE categoryid = $catid 
    WHERE height = 1080 
    OR width = 2560 
    OR width = 1366 
    OR height = 720 
    OR width = 2560 
    AND height = 1600 
    OR width = 1680 
    AND height = 1050 
    OR width = 1920 
    AND height = 1200 
    OR width = 1280 
    AND height = 800 
    OR width = 1440 
    AND height = 900 
    ORDER BY wallpaperid DESC 
    LIMIT $from,$max_results 
"; 

我该如何解决这个问题?

+2

两个WHERE子句中的语句,然后AND和OR的组合(不带任何括号),看起来就像你不懂布尔逻辑一样。 – 2011-04-25 11:39:02

+0

如何修复此代码? – Hassan 2011-04-25 11:49:42

回答

2

这是因为你的SQL是错误的:

select * from wallpaper 
    where categoryid = $catid 
    where height = 1080 OR width = 2560 OR width = 1366 
    OR height = 720 OR width = 2560 AND height = 1600 
    OR width = 1680 AND height = 1050 OR width = 1920 
    AND height = 1200 OR width = 1280 AND height = 800 
    OR width = 1440 AND height = 900 
    order by wallpaperid desc 
    limit $from,$max_results 

你在那里有两个WHERE秒。您应该使用AND代替第二个,也许将所有内容都包装成parens。

但也可以尝试在脚本中使用print mysql_error()。尤其对于某些不起作用的调试。 MySQL会在大多数时候告诉你你的查询有什么问题。


很好。您的重写查询:

select * from wallpaper 
    where categoryid = $catid 
    AND (
     height = 1080 OR width = 2560 OR width = 1366 
    OR height = 720 OR width = 2560 AND height = 1600 
    OR width = 1680 AND height = 1050 OR width = 1920 
    AND height = 1200 OR width = 1280 AND height = 800 
    OR width = 1440 AND height = 900 
     ) 
    order by wallpaperid desc 
    limit $from,$max_results 

但你可能需要使用简单的是:

SELECT * FROM wallpaper 
    WHERE categoryid = $catid 
    AND width IN (1440, 2560, 1366, 1680, 1920, 1280) 
    AND height IN (1200, 800, 900, 1080, 720, 1600, 1050) 
    ORDER BY wallpaperid DESC 
    LIMIT $from,$max_results 
+0

那么如何解决它? – Hassan 2011-04-25 11:49:09

+1

已经告诉过你。 – mario 2011-04-25 11:49:59

+0

PLease把一个固定代码这是它的工作罚款..如果第二个WHERE我没有使用然后我使用或如果我使用大括号然后我在这个代码中使用它 – Hassan 2011-04-25 11:57:27