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

发布于 2024-09-08 19:02:32 字数 1729 浏览 2 评论 0原文

我正在尝试对表中的列进行 suma,同时排除付费字段设置为 true 的某些记录。

我这样做是这样的:

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

表中充满了数据,我可以单独显示成本或整个总计。只是添加 并支付<>1 是什么导致它失败。查询不会因此失败,但会返回 NULL,这是毫无用处的。

这是我的表的 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)
)

和当前数据

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');

I am trying to suma column in a table, while excluding certain records that have the paid field set to 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. The query does not fail as such, but NULL is returned which is quite useless.

This is the SQL for my table

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');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冷心人i 2024-09-15 19:02:33

这可能意味着表中不存在已付费<>的行。 1. 或者也许有,但它们的成本为 NULL。

This may mean there are no rows in the table for which paid <> 1. Or maybe there are but they cost of NULL.

牵强ㄟ 2024-09-15 19:02:33

我不会使用“<>”,而是使用“!=”;

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

如果这不起作用,您可以发布表定义吗?还有没有付款=1的记录吗?

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?

我不在是我 2024-09-15 19:02:32

该行的“已付费”值为 NULL。您需要这样做

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*/

,或者,更好的是不允许该列中存在 NULL,并将默认值设置为 0。

The 'paid' value is NULL for that row. 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.

时光暖心i 2024-09-15 19:02:32

您的问题是您的条件与任何行都不匹配。

条件已支付<> 1paidNULL 的行不匹配。

试试这个查询:SELECT 1 <> NULL 它将返回NULLWHERE 子句会过滤掉该子句为 false 或 NULL 的行。

替换并付费<> 1AND(付费 IS NULL 或付费 <> 1)

Your problem is that your condition does not match any rows.

The condition paid <> 1 does not match the row where paid is NULL.

Try this query: SELECT 1 <> NULL It will return NULL. A WHERE clause filters out rows in which the clause is either false or NULL.

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

荒岛晴空 2024-09-15 19:02:32

SQL Antipatterns一书详细描述了这个问题,Searching Nullable列。强烈推荐的书。

The book SQL Antipatterns describes this problem in detail, section Searching Nullable Columns. Strongly recommended book.

信仰 2024-09-15 19:02:32
SELECT SUM( cost ) AS total
FROM sales
WHERE passport = 'xxxxx'
AND paid = false or paid is null

编辑

使用

IS NOT 

而不是

!=

这应该有效

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

EDITED

use

IS NOT 

instead of

!=

This should work

SELECT SUM( cost ) AS total
FROM sales
WHERE passport = 'xxxxx'
AND paid IS NOT true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文