我有两张桌子:
bid_tag
bid_tag_design
bid_tag: id,tag_design,tag_size,user_id,reserved
big_tag_design: id,name
我希望能够在bid_tag表WHERE user_id = 0 AND reserved = 0
然后,我将使用该查询为每日库存水平报告创建CRON
作业...
答案 0 :(得分: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 join bid_tag_design btd 在bt.bid_tag_id = btd.bid_tag_id 其中bt.user_id = 0且btd.reserved = 0
答案 1 :(得分: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
答案 2 :(得分:0)
试试这个
select tag_design ,count(tag_design) countdesign
from bid_tag
where user_id = 0 and reserved = 0
group by tag_design