2013-02-22 61 views
0

這就是我想要達到的目標:選擇行是否滿足第一條件並丟棄下列條件

SELECT * 
FROM ventas WHERE (
posicion = 'cc21' 
AND inic < NOW() 
AND fin > NOW() 
) ELSE (
WHERE posicion = 'cc21' 
AND fijo = 1) 
ELSE (WHERE posicion = 'cc21' 
AND hits < limite) 
AND contenido = 'notas' 
LIMIT 1 

我試過JOIN和其他例子在這個網站,但非似乎指向我右側對於這種特殊情況下的方向,任何幫助表示讚賞:)

這dindn't工作

SELECT 
     TOP (1) * 
    FROM 
     ventas 
    WHERE posicion = 'cc21' 
     AND (inic < NOW() AND fin > NOW()) 
     OR (fijo = 1) 
     OR (hits < limite) 
     AND contenido = 'notas' 
    LIMIT 1 

回答

1

你可以直接使用OR

SELECT * 
FROM ventas 
WHERE 
     (
      (posicion = 'cc21' AND inic < NOW() AND fin > NOW()) 
      OR 
      (posicion = 'cc21' AND fijo = 1) 
      OR 
      (posicion = 'cc21' AND hits < limite) 
     ) 
     AND 
     contenido = 'notas' 
LIMIT 1 
+0

我試過,但如果前一行遇到第二個條件比我沒有得到我想要的 – 2013-02-22 05:38:51

+0

你能給樣本記錄與期望的結果? – 2013-02-22 05:39:40

+0

如果您執行查詢,結果如何?定義* ..我沒有得到我想要的東西* – 2013-02-22 05:40:13

0

您需要使用TOP子句獲取第一行。

至於其餘的查詢,我很難理解它。但是,您需要使用AND和OR運算符來完成這項工作。您可以使用圓括號(括號)對它們進行分組。

SELECT TOP(1)* FROM塔斯 WHERE職位= 'CC21' AND((A和B)或(C)或(d))

希望這有助於。

0

跳水後,最後也是沒有找到魚我使出這樣的:

$cc21 = mysqli_query($not,"SELECT * FROM ventas WHERE posicion = 'cc21' 
AND inic < NOW() AND fin > NOW() and contenido = 'notas' LIMIT 1"); 
if (mysqli_num_rows($cc21) == 0) { 
$cc21 = mysqli_query($not,"SELECT * FROM ventas WHERE posicion = 'cc21' 
AND fijo = '1' and contenido = 'notas' LIMIT 1"); 
if (mysqli_num_rows($cc21) == 0) { 
$cc21 = mysqli_query($not,"SELECT * FROM ventas WHERE posicion = 'cc21' AND hits < limite AND contenido = 'notas' LIMIT 1"); 
} 
} 

這是不乾淨的,也不緊湊,但它能夠完成任務;)

坦克大家的寶貴幫助