选择排除表中特定值的查询

时间:2014-07-16 05:05:41

标签: php mysql select

我有一个查询,它会计算具有值' 1234'在' dealer_code'字段。

这样可以正常工作,但我不希望包含带有' sales_id'其中包含或以#39; .D999'的值结尾。

例如,如果数据库中的sales_id包含以下内容:

sales_id  |  dealer_code
123.D000  |  1234          x
123.D777  |  1234          x
123.D333  |  5555 
123.D999  |  1234 
123.D999  |  5555  
123.D111  |  1234          x
123.D999  |  5555  

下面的代码应该显示3的结果,但它说4.可以有人帮忙吗?

$total_query = "SELECT COUNT(sales_points) AS total_sales_points 
FROM sales_list 
WHERE dealer_code = '1234'
AND sales_id != '.D999'
AND sales_entry_date BETWEEN '2013-11-01 00:00:00' AND '2014-11-31 23:59:59'";

4 个答案:

答案 0 :(得分:3)

试试这个:

$total_query = "SELECT COUNT(sales_points) AS total_sales_points 
                FROM sales_list 
                WHERE dealer_code = '1234'
                AND sales_id NOT LIKE '%.D999'
                AND sales_entry_date BETWEEN '2013-11-01 00:00:00' AND '2014-11-31 23:59:59'";

答案 1 :(得分:0)

SELECT COUNT(sales_points) AS total_sales_points 
FROM sales_list 
WHERE dealer_code = '1234'
AND sales_id NOT LIKE '%.D999%'
AND sales_entry_date BETWEEN '2013-11-01 00:00:00' AND '2014-11-31 23:59:59'";

答案 2 :(得分:0)

$total_query = "SELECT COUNT(sales_points) AS total_sales_points 
FROM sales_list 
WHERE dealer_code = '1234'
AND sales_id NOT LIKE '%.D999'
AND sales_entry_date BETWEEN '2013-11-01 00:00:00' AND '2014-11-31 23:59:59'";

答案 3 :(得分:0)

我认为查询应如下所示。

  $total_query = "SELECT COUNT(sales_points) AS total_sales_points 
    FROM sales_list 
    WHERE dealer_code = '1234'
    AND sales_id NOT LIKE '%.D999%'
    AND sales_entry_date BETWEEN '2013-11-01 00:00:00' AND '2014-11-31 23:59:59'";