2011-04-25 62 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