为什么SELECT查询没有选择应该选择的内容?

时间:2015-11-04 15:52:27

标签: php mysql sql

我从未在之前经历过这种情况,所以我想知道在代码中要照顾什么

这是代码 - 应该有用吗?

    $time = strtotime("-$a2 year", time());
$date = date("Y", $time);

$query = "SELECT * FROM users WHERE country='$country' AND state='$state'";

if(!empty($a2) && empty($a1)){
  $query .= " AND year<=";
  $query .= " '$date'";
  $query .= " AND day<=";
  $query .= " '$todayd'";
  $query .= " AND month<=";
  $query .= " '$todaym'";
 }
if(..){}
if(..){}
     $result = $query;

    $query = mysqli_query($con,"$result");

    while($row = mysqli_fetch_array($query))
    {
    .......

如果我回显字符串$ query,则会显示:

SELECT * FROM users WHERE country='Spain' AND state='Madrid'

但是所选/显示的用户拥有各种不同的状态和状态

1 个答案:

答案 0 :(得分:-1)

来自作者评论:

  

是的我有1或2个OR条件。为什么呢?

SELECT * FROM users WHERE country='$country' AND state='$state' OR ...

布尔逻辑说:

(p1 AND p2) OR p3

如果p3为真,则整个条件为真。这种情况是你获得p3满足的(p1和p2)+记录的行。

<强> Disjunction

  如果A为真,或者如果B为真,或者如果A和B都为真,则“A或B”为真   真。

示例:

CREATE TABLE #tab(country VARCHAR(100), state VARCHAR(100));

INSERT INTO #tab
VALUES ('Spain', 'Madrid'), ('Russia','Moscow');

SELECT *
FROM #tab
WHERE country='Spain' AND state='Madrid' OR 4 > 2

LiveDemo