SQL左JOIN不提取预期数据

发布于 2025-02-10 12:01:01 字数 2030 浏览 1 评论 0 原文

我有以下表格:

CATEGORIES:
id | name          | category_group  | cate_type_id
1  | Entertainment | Entertainment   | 1
2  | Electricity   | Utilities       | 8
3  | Water         | Utilities       | 8
4  | Rent          | Living Exp      | 6
5  | credit card   | Finance         | 5

BUDGET-ITEMS:
id | budget_id | cat_id | category_group| budget_yr | budget_01 | budget_02 | ... | budget_12
1  | 1         | 1      | Entertainment | 2022      | 500       |           |
2  | 1         | 2      | Utilities     | 2022      | 1500      |           |
3  | 1         | 3      | Utilities     | 2022      |           |  250      |

我想从类别表中拉出所有项目,并带有映射预算列。以下是我的加入。

SELECT c.id as base_id,c.name,c.category_type_id, c.category_group as base_group, b.* 
FROM category c 
LEFT JOIN budget_items b ON c.id = b.category_id 
WHERE c.category_type_id NOT IN (5) 
ORDER BY c.category_type_id, c.category_group ASC

我希望下面的输出:

id | budget_id | cat_id | catgroup      | budget_yr | budget_01 | budget_02 | ... | budget_12
1  | 1         | 1      | Entertainment | 2022      | 500       |           |
2  | 1         | 2      | Utilities     | 2022      | 1500      |           |
3  | 1         | 3      | Utilities     | 2022      |           |  250      |
4  | 1         | 4      | Living Exp    | 2022      |           |           |

但是,我会像下面的那样(在此处截断的基本*列*):

id | budget_id | cat_id | catgroup      | budget_yr | budget_01 | budget_02 | ... | budget_12
1  | 1         | 1      | Entertainment | 2022      | 500       |           |
2  | 1         | 2      | Utilities     | 2022      |           |  1500     |
3  | 1         | 3      | Utilities     | 2022      |           |           |
4  | 1         | 4      | Living Exp    | 2022      |           |           |

我的查询看起来还不错,不确定它在哪里出错。有人看到这个问题吗?

预先感谢您的帮助。

编辑: 我在这里截断了一些空间的列。问题是值与不同的预算列保持一致。我从左表正确获取列。

编辑: 多亏了所有寻求帮助的人,我终于发现问题是我的数据。查询实际上工作正常。这个社区很棒。

I have the below tables:

CATEGORIES:
id | name          | category_group  | cate_type_id
1  | Entertainment | Entertainment   | 1
2  | Electricity   | Utilities       | 8
3  | Water         | Utilities       | 8
4  | Rent          | Living Exp      | 6
5  | credit card   | Finance         | 5

BUDGET-ITEMS:
id | budget_id | cat_id | category_group| budget_yr | budget_01 | budget_02 | ... | budget_12
1  | 1         | 1      | Entertainment | 2022      | 500       |           |
2  | 1         | 2      | Utilities     | 2022      | 1500      |           |
3  | 1         | 3      | Utilities     | 2022      |           |  250      |

I want to pull all items from Category table with mapping budget columns. Below is my JOIN.

SELECT c.id as base_id,c.name,c.category_type_id, c.category_group as base_group, b.* 
FROM category c 
LEFT JOIN budget_items b ON c.id = b.category_id 
WHERE c.category_type_id NOT IN (5) 
ORDER BY c.category_type_id, c.category_group ASC

I expect the below output:

id | budget_id | cat_id | catgroup      | budget_yr | budget_01 | budget_02 | ... | budget_12
1  | 1         | 1      | Entertainment | 2022      | 500       |           |
2  | 1         | 2      | Utilities     | 2022      | 1500      |           |
3  | 1         | 3      | Utilities     | 2022      |           |  250      |
4  | 1         | 4      | Living Exp    | 2022      |           |           |

However, I get like below (truncated base* columns here for space):

id | budget_id | cat_id | catgroup      | budget_yr | budget_01 | budget_02 | ... | budget_12
1  | 1         | 1      | Entertainment | 2022      | 500       |           |
2  | 1         | 2      | Utilities     | 2022      |           |  1500     |
3  | 1         | 3      | Utilities     | 2022      |           |           |
4  | 1         | 4      | Living Exp    | 2022      |           |           |

My query looks OK, not sure where it is going wrong. Does anyone see the issue?

Thanks in advance for your kind help.

Edit:
I have truncated some columns for space here. the problem is the values are aligned to different budget columns. I get the columns correctly from the left table.

Edit:
Thanks to everyone who pitched in to help, I finally figured the issue was with my data. The query was actually working fine. This community is amazing.

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

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

发布评论

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

评论(1

青萝楚歌 2025-02-17 12:01:01

不确定您在做什么,但似乎对我的预期工作:

架构(sqlite v3.30)

CREATE TABLE items (
  `id` INTEGER,
  `budget_id` INTEGER,
  `cat_id` INTEGER,
  `catgroup` VARCHAR(13),
  `budget_yr` INTEGER,
  `budget_01` INTEGER,
  `budget_02` INTEGER
);

INSERT INTO items
  (`id`, `budget_id`, `cat_id`, `catgroup`, `budget_yr`, `budget_01`, `budget_02`)
VALUES
  ('1', '1', '1', 'Entertainment', '2022', '500', null),
  ('2', '1', '2', 'Utilities', '2022', '1500', null),
  ('3', '1', '3', 'Utilities', '2022', null, '250');

CREATE TABLE cats (
  `id` INTEGER,
  `name` VARCHAR(13),
  `category_group` VARCHAR(13),
  `cate_type_id` INTEGER
);

INSERT INTO cats
  (`id`, `name`, `category_group`, `cate_type_id`)
VALUES
  ('1', 'Entertainment', 'Entertainment', '1'),
  ('2', 'Electricity', 'Utilities', '8'),
  ('3', 'Water', 'Utilities', '8'),
  ('4', 'Rent', 'Living Exp', '6'),
  ('5', 'credit card', 'Finance', '5');

查询

SELECT c.id
     , budget_id
     , cat_id
     , catgroup
     , budget_yr
     , budget_01
     , budget_02
FROM cats c
LEFT JOIN items i ON c.id = i.cat_id
WHERE c.cate_type_id <> 5;
id buccatig_id cat_id cat_id catgroup bucvent_yr bucvent_yr judgem_01 judge_02
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1娱乐 2022 500
2 1 2 UTILITION 2022 1500
3 1 3 UTITIOR 2022 250
4

Not sure what you're doing, but it seems to work as expected for me:

Schema (SQLite v3.30)

CREATE TABLE items (
  `id` INTEGER,
  `budget_id` INTEGER,
  `cat_id` INTEGER,
  `catgroup` VARCHAR(13),
  `budget_yr` INTEGER,
  `budget_01` INTEGER,
  `budget_02` INTEGER
);

INSERT INTO items
  (`id`, `budget_id`, `cat_id`, `catgroup`, `budget_yr`, `budget_01`, `budget_02`)
VALUES
  ('1', '1', '1', 'Entertainment', '2022', '500', null),
  ('2', '1', '2', 'Utilities', '2022', '1500', null),
  ('3', '1', '3', 'Utilities', '2022', null, '250');

CREATE TABLE cats (
  `id` INTEGER,
  `name` VARCHAR(13),
  `category_group` VARCHAR(13),
  `cate_type_id` INTEGER
);

INSERT INTO cats
  (`id`, `name`, `category_group`, `cate_type_id`)
VALUES
  ('1', 'Entertainment', 'Entertainment', '1'),
  ('2', 'Electricity', 'Utilities', '8'),
  ('3', 'Water', 'Utilities', '8'),
  ('4', 'Rent', 'Living Exp', '6'),
  ('5', 'credit card', 'Finance', '5');

Query

SELECT c.id
     , budget_id
     , cat_id
     , catgroup
     , budget_yr
     , budget_01
     , budget_02
FROM cats c
LEFT JOIN items i ON c.id = i.cat_id
WHERE c.cate_type_id <> 5;
id budget_id cat_id catgroup budget_yr budget_01 budget_02
1 1 1 Entertainment 2022 500
2 1 2 Utilities 2022 1500
3 1 3 Utilities 2022 250
4

View on DB Fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文