简体   繁体   English

对字段求和,同时排除SQL中的某些字段

[英]Summing a field while excluding certain fields in SQL

I am trying to suma column in a table, while excluding certain records that have the paid field set to true. 我试图对表中的列求和,同时排除某些将付费字段设置为true的记录。

I am doing so like this: 我这样做是这样的:

SELECT SUM( cost ) AS total
FROM sales
WHERE passport = 'xxxxx'
AND paid <>1

The table is full of data, and I can display costs by themselves, or the entire total. 该表中有很多数据,我可以自己显示费用,也可以显示全部费用。 Just adding on AND paid <>1 Is what causes it to fail. 仅仅加上AND paid <> 1就是导致失败的原因。 The query does not fail as such, but NULL is returned which is quite useless. 查询不会因此失败,但是会返回NULL,这是完全没有用的。

This is the SQL for my table 这是我的表的SQL

CREATE TABLE sales (
  id int(10) unsigned NOT NULL AUTO_INCREMENT,
  uuid varchar(64) NOT NULL,
  firstname varchar(64) NOT NULL DEFAULT '',
  lastname varchar(64) NOT NULL DEFAULT '',
  passport varchar(64) DEFAULT NULL,
  product varchar(64) NOT NULL,
  quantity int(11) DEFAULT NULL,
  cost double DEFAULT NULL,
  paymenttype varchar(64) NOT NULL DEFAULT '',
  paid tinyint(1) DEFAULT NULL,
  tabno varchar(64) NOT NULL,
  createdby int(10) unsigned DEFAULT NULL,
  creationdate datetime DEFAULT NULL,
  modifiedby int(10) unsigned DEFAULT NULL,
  modifieddate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id)
)

And the current data 和当前数据

INSERT INTO sales (id, uuid, firstname, lastname, passport, product, quantity, cost, paymenttype, paid, tabno, createdby, creationdate, modifiedby, modifieddate) VALUES
(20, ':8dcee958-d1ac-6791-6253-0a7344054295', 'Jason', 'Hoff', 'r454545', 'Nicaraguan nachoes', 4, 320, 'credit', 1, '23434', 2, '2010-07-06 04:10:18', 2, '2010-07-06 04:10:18'),
(19, ':3f03cda5-21bf-9d8c-5eaa-664eb2d4f5a6', 'Jason', 'Hoff', 'r454545', 'Nica Libre (doble 4 o 5 anos)', 1, 30, 'cash', NULL, '35', 2, '2010-07-06 03:35:35', 2, '2010-07-06 03:35:35'),
(18, ':f83da33b-2238-94b9-897c-debed0c3815e', 'Jason', 'Hoff', 'r454545', 'Helado con salsa de chocolate', 1, 40, 'cash', 1, '2', 2, '2010-07-05 21:30:58', 2, '2010-07-05 21:30:58');

The 'paid' value is NULL for that row. 该行的“已付费”值为NULL。 You would need to do 你需要做

SELECT SUM( cost ) AS total
FROM test.sales
WHERE passport = 'r454545'
AND paid IS NULL or paid = 0 
     /*Or paid <> 1 as I see you are using tinyint datatype*/

Or, better would be to not allow NULLS in that column and have paid default to 0. 或者,最好是在该列中不允许使用NULLS并将默认值设置为0。

Your problem is that your condition does not match any rows. 您的问题是您的条件与任何行都不匹配。

The condition paid <> 1 does not match the row where paid is NULL . 条件paid <> 1不匹配,其中行paidNULL

Try this query: SELECT 1 <> NULL It will return NULL . 尝试以下查询: SELECT 1 <> NULL它将返回NULL A WHERE clause filters out rows in which the clause is either false or NULL . WHERE子句过滤掉该子句为false或NULL

Replace AND paid <> 1 with AND (paid IS NULL OR paid <> 1) AND paid <> 1替换为AND (paid IS NULL OR paid <> 1)

The book SQL Antipatterns describes this problem in detail, section Searching Nullable Columns . SQL反模式 》一书在“ 搜索可为空的列”一节中详细介绍了此问题。 Strongly recommended book. 强烈推荐这本书。

这可能意味着表中没有支付<> 1的行。或者也许有,但是它们的成本为NULL。

I would not use "<>", but use "!=" instead; 我不会使用“ <>”,而是使用“!=”;

SELECT SUM( cost ) AS total
FROM sales
WHERE passport = 'xxxxx'
AND paid != 1

If that doesn't work, can you post the table definition? 如果这不起作用,您可以发布表定义吗? And are there any records which do not have paid = 1? 有没有未支付的记录= 1?

SELECT SUM( cost ) AS total
FROM sales
WHERE passport = 'xxxxx'
AND paid = false or paid is null

EDITED EDITED

use 采用

IS NOT 

instead of 代替

!=

This should work 这应该工作

SELECT SUM( cost ) AS total
FROM sales
WHERE passport = 'xxxxx'
AND paid IS NOT true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM