-2

我有两张桌子:

  • bid_tag
  • bid_tag_design

    bid_tag:id、tag_design、tag_size、user_id、保留

    big_tag_design:ID、名称

我希望能够查询 bid_tag 表中每个标签设计的数量WHERE user_id = 0 AND reserved = 0

然后,我将使用该查询为CRON库存水平的每日报告创建一个作业...

4

3 回答 3

1

如果您只想要计数而不是按标签计数,那么类似:

select count(*)
from bid_tag_design
where user_id = 0 and reserved = 0

这样做,假设 user_id 和 reserved 在该表中。如果bid_tag 具有这些字段中的一个或两个,则仅需要加入,例如:

select count(*)
from bid_tag bt join
     bid_tag_design btd
     on bt.bid_tag_id = btd.bid_tag_id
where bt.user_id = 0 and btd.reserved = 0

如果您希望每个设计都有一行,请按以下方式分组:

select btd.bid_tag_design_id, count(*)

  从 bid_tag bt 加入 bid_tag_design btd on bt.bid_tag_id = btd.bid_tag_id 其中 bt.user_id = 0 和 btd.reserved = 0

于 2012-12-01T18:20:49.440 回答
0

尝试这个

     select tag_design ,count(tag_design) countdesign
     from bid_tag
     where user_id = 0 and reserved = 0
     group by tag_design
于 2012-12-01T18:26:49.780 回答
0

在您发表评论之后,表格之间的链接似乎是相反的。因此,我更改了查询以将架构考虑在内,它应该可以使用:

SELECT COUNT(*) as nbDesigns, bid_tag_design.id, bid_tag_design.name
FROM bid_tag_design
  LEFT JOIN bid_tag ON bid_tag.tag_design = bid_tag_design.id
WHERE bid_tag.user_id = 0 AND bid_tag.reserved = 0
GROUP BY bid_tag.tag_design

假设您在 bid_tag_design 中有这个:

id | name
1  | design 1
2  | design 2

然后在bid_tag中

id | tag_design | user | reserved
1  | 1          | 0    | 0
2  | 1          | 0    | 0
3  | 2          | 0    | 0
4  | 2          | 0    | 0
5  | 1          | 1    | 0
6  | 2          | 2    | 0
7  | 1          | 0    | 0
8  | 1          | 0    | 0
9  | 1          | 3    | 0

那么第二次查询的结果应该是

nbDesigns | id | name
4         | 1  | design 1
2         | 2  | design 2
于 2012-12-01T18:10:00.263 回答