从4个不同的sql表中选择count语句

时间:2018-03-15 15:10:09

标签: sql

我有4个表格,结构如下:

**FiltersMain**
FilterMain_ID
FilterMain_Name_GR
FilterMain_Name_EN

**FiltersSub**
FilterSub_ID
FilterMain_ID
FilterSub_Name_GR
FilterSub_Name_EN

**cm**
cm_ID
product_id
Cat_Main_ID


**Filtra**
f_Id
product_id
FilterMain_ID
FilterSub_ID

我现在唯一知道的事情如下:

FilterMain_ID表格的FiltersMain(555)

Cat_Main_ID表的

cm(即222)

我想(使用select count方法)计算Filtra表中的记录总数但是具有以下条件:

拥有FilterMain_ID表(555)的已知FiltersMainCat_Main_ID表的已知cm(即222)。

我已经尝试了以下select count语句,但结果完全错误。

SELECT COUNT(*) AS total_records

FROM

FiltersMain,
FiltersSub,
Filtra,
cm

WHERE

FiltersMain.FilterMain_ID = FiltersSub.FilterMain_ID
Filtra.FilterMain_ID = FiltersMain.FilterMain_ID
cm.Cat_Main_ID = 222
cm.product_id = Filtra.product_id

此刻我完全感到困惑,所以任何帮助都会非常感激

2 个答案:

答案 0 :(得分:3)

这可能是您要找的?

SELECT COUNT(fa.f_Id) AS total_records
FROM Filtra fa
LEFT JOIN FiltersMain fm ON fm.FilterMain_ID = fa.FilterMain_ID
LEFT JOIN FiltersSub fs ON fs.FilterSub_ID = fa.FilterSub_ID
LEFT JOIN cm c ON c.product_id = fa.product_id AND c.Cat_Main_ID = 222

答案 1 :(得分:0)

{{1}}