2013-02-22 62 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"); 
} 
} 

这是不干净的,也不紧凑,但它能够完成任务;)

坦克大家的宝贵帮助