有如下几个表,亦或者用程序方法实现,要求效率高,求较好方案。
表一,等级表shop_brand_level:
brand_level_id-----brand_level_name
---1-----------A
---2-----------B
---3-----------C
---------------------------------------------------------------------
表二,品牌表shop_brand:
brand_id-----brand_name-----brand_level_id(对应表level的id)
---1-----------波司登---------------3
---2-----------酷卡-----------------2
---3 ----------羽杰-----------------1
---------------------------------------------------------------------
表三,位置表shop_location,无限级分类:
location_id-----location_name-----parent_id
---1-----------------西---------------0
---2-----------------北---------------0
---3-----------------东---------------0
---4-----------------南---------------0
---------------------------------------------------------------------
表四,信息表shop_info:
shop_id-------------name------location------brand_id(对应表brand的id,可能存在多选,逗号分隔。)
---1---------------李杰-----------2-----------2
---2---------------张英-----------3-----------3
---3---------------郭真-----------1-----------2,3
---------------------------------------------------------------------
以上是四个表的大概结构,现在要求以下统计数据,各位置各有多少等级的数量。
等级\位置----------东------------南------------西------------北
---A---------------10-------------2-------------4-------------6
---B---------------20-------------3-------------5-------------6
---C---------------21-------------4-------------4-------------5
---D---------------22-------------5-------------4-------------5
以上,谢谢解答。
sql:
select A.level_name as 等级, C.location_name 位置 from level as A, brand AS B, location as C, info AS D
where A.level_id = B.level_id AND D.brand_id LIKE concat('%',D.brand_id,'%') AND C.location_id = D.location
查询出来后,建立字典类型为Dictionary<string, Dictionary<string, int>>,循环读取查询结果,每读一条出来判断字典中是否存在,不存在添加新纪录并设置次数为1,存在则在原值上加1
安照这个查询语句,倒是可以执行,但是返回的数据感觉是错误的,能解释一下吗。
select
A.brand_level_id,A.brand_level_name,C.location_id,C.location_name,D.locat
ion_f,count(D.location_f) as count
from shop_brand_level A,shop_brand B, shop_location C, shop_info D
where A.brand_level_id = B.brand_level_id
AND B.brand_id LIKE concat('%',D.main_brand,'%')
AND C.location_id = D.location
group by A.brand_level_id,D.location_f
order by A.brand_level_id,D.location_f
这样查询有问题吗?
东西南北?
所在位置
一个产品信息,对应多个brand_id何解?同时,怎样算这个产品的level?
一个信息可能含有多个品牌的。
@夜落朦空:可每个品牌的level不一致,那这条信息算哪个level呢?
select
A.brand_level_id,A.brand_level_name,C.location_id,C.location_name,D.locat
ion_f,count(D.location_f) as count
from shop_brand_level A,shop_brand B, shop_location C, shop_info D
where A.brand_level_id = B.brand_level_id
AND B.brand_id LIKE concat('%',D.main_brand,'%')
AND C.location_id = D.location
group by A.brand_level_id,D.location_f
order by A.brand_level_id,D.location_f
@夜落朦空: 这...