当我认为他们应该给我相同的结果时,我需要一些帮助来理解为什么这两个陈述给我两个不同的结果:
SELECT Field1, SUM(Field2+Field3+Field4) AS AggNum
FROM Table1
WHERE Filter1 IN ('a')
AND (Filter2 IN ('x','y') OR (Filter2 ='z' AND Filter3 = 'xxx'))
GROUP BY Field1
VS
SELECT Field1, SUM(Field2+Field3+Field4) AS AggNum
FROM Table1
WHERE Filter1 IN ('a')
AND Filter2 IN ('x','y') OR (Filter2 ='z' AND Filter3 = 'xxx')
AND Filter4 = 'd'
GROUP BY Field1
第二个查询给了我正确的结果,但我不明白这两个陈述在逻辑上是如何不同的。在我看来,where子句中AND语句周围的()根本不重要。
这只是因为SQL Server按顺序处理它吗?
如果我做了类似的事情会发生什么:
<?php
namespace MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Commande
*
* @ORM\Table(name="commande")
* @ORM\Entity(repositoryClass="MainBundle\Repository\CommandeRepository")
*/
class Commande
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var bool
*
* @ORM\Column(name="valider", type="boolean")
*/
private $valider;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* @var string
*
* @ORM\ManyToOne(targetEntity="UserBundle\Entity\User", inversedBy="commandes")
*/
private $user;
/**
* @var int
*
* @ORM\Column(name="reference", type="integer", nullable=true)
*/
private $reference;
/**
* @var array
*
* @ORM\Column(name="commande", type="array", nullable=true)
*/
private $commande;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set valider
*
* @param boolean $valider
*
* @return Commande
*/
public function setValider($valider)
{
$this->valider = $valider;
return $this;
}
/**
* Get valider
*
* @return bool
*/
public function getValider()
{
return $this->valider;
}
/**
* Set date
*
* @param \DateTime $date
*
* @return Commande
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set reference
*
* @param integer $reference
*
* @return Commande
*/
public function setReference($reference)
{
$this->reference = $reference;
return $this;
}
/**
* Get reference
*
* @return int
*/
public function getReference()
{
return $this->reference;
}
/**
* Set products
*
* @param array $products
*
* @return Commande
*/
public function setProducts($products)
{
$this->products = $products;
return $this;
}
/**
* Get products
*
* @return array
*/
public function getProducts()
{
return $this->products;
}
/**
* Set user
*
* @param \UserBundle\Entity\User $user
*
* @return Commande
*/
public function setUser(\UserBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set commande
*
* @param array $commande
*
* @return Commande
*/
public function setCommande($commande)
{
$this->commande = $commande;
return $this;
}
/**
* Get commande
*
* @return array
*/
public function getCommande()
{
return $this->commande;
}
}
如此简化,它将是A和B或C和D ......这是否意味着查询将返回仅为C和D的结果?
答案 0 :(得分:2)
我将继续并假设在您的实际代码中您有必要的右括号,以便没有语法错误。
使用字母来表示条件块,我们可以将您的问题浓缩为此,
WHERE A AND B OR C
VS
WHERE A AND (B OR C)
在第二个陈述中,对于要通过的条件,A必须为真。
在第一个陈述中,只要C为真,A或B是否为真无关紧要。
答案 1 :(得分:0)
Farellia。 区别在于第一个语句中的最后一个条件是“OR”运算符,因此取消了其他条件。