我想选择所有类别,子类别和计数业务属于子类别的数量。 这是SQL查询我使用。
SELECT
c.id,
c.name,
c.slug,
sc.id,
sc.name,
sc.slug,
COUNT(bsc.id) AS business_count
FROM
fi_category c
LEFT JOIN
fi_subcategory sc ON c.id = sc.category_id AND (sc.deleted_at IS NULL)
LEFT JOIN
fi_business_subcategory bsc ON sc.id = bsc.subcategory_id AND (bsc.deleted_at IS NULL)
WHERE
(c.deleted_at IS NULL)
GROUP BY
c.id, sc.id
但有更我想做的事情,BUSINESS_COUNT应根据我想选择所有类别,子类别,但BUSINESS_COUNT应该有一个像子句他们属于中端,即全市过滤WHERE city.id = 1
,为了这个,我想我必须使用数作为子查询对此我没有能够搞清楚。
下面是从关系结构fi_business_subcategory
到fi_city
。
1) fi_business_subcategory
+----+----------------+-------------+
| id | subcategory_id | business_id |
+----+----------------+-------------+
2) fi_business
+----+---------+-----------+
| id | name | suburb_id |
+----+---------+-----------+
3) fi_suburb
+-----+--------+---------+
| id | name | city_id |
+-----+--------+---------+
4) fi_city
+----+--------+
| id | name |
+----+--------+
我想这样的事情,但是这似乎并没有工作
SELECT
c.id,
c.name,
c.slug,
sc.id,
sc.name,
sc.slug,
bsc.business_count
FROM
fi_category c
LEFT JOIN
fi_subcategory sc ON c.id = sc.category_id AND (sc.deleted_at IS NULL)
LEFT JOIN (
SELECT
COUNT(business_id) t1.business_count, t1.subcategory_id
FROM
fi_business_subcategory t1
LEFT JOIN
fi_business t2 ON t2.id = t1.business_id
LEFT JOIN
fi_suburb t3 ON t3.id = t2.suburb_id
LEFT JOIN
fi_city t4 ON t4.id = t3.city_id
WHERE
t4.id = 1
GROUP BY
t1.subcategory_id
) bsc ON sc.id = bsc.subcategory_id AND (bsc.deleted_at IS NULL)
WHERE
(c.deleted_at IS NULL)
GROUP BY
c.id, sc.id
我应该如何建立查询,以实现我想要什么?
我看不出有什么理由你应该使用子查询。 我相信,你可以简单地结合起来fi_business
和fi_business_subcategory
到一个括号中的表的因素。
SELECT
c.id,
c.name,
c.slug,
sc.id,
sc.name,
sc.slug,
COUNT(bsc.id) AS business_count
FROM
fi_category c
LEFT JOIN
fi_subcategory sc ON c.id = sc.category_id AND (sc.deleted_at IS NULL)
LEFT JOIN (
fi_business b
INNER JOIN
fi_business_subcategory bsc ON b.id = bsc.business_id AND (bsc.deleted_at IS NULL)
INNER JOIN
fi_suburb su ON su.id = b.suburb_id AND su.city_id = 1
) ON sc.id = bsc.subcategory_id
WHERE
(c.deleted_at IS NULL)
GROUP BY
c.id, sc.id
我检查 ,这是对你的表结构有效的SQL。 我想有很好的机会,这将产生预期的结果,即使你的小提琴不包含任何数据。 见在JOIN语法手册上,您可以在一个连接使用括号的详细信息。
你也可以问问自己,如果你真的需要所有的连接要留给连接。 使用内部书写的东西加入就好办多了。
随着加入被执行左到右,你可能会做的内连接第一,其次是序列的正确连接。 这避免了括号:
SELECT
c.id cat_id,
c.name cat_name,
c.slug cat_slug,
sc.id sub_id,
sc.name sub_name,
sc.slug sub_slug,
COUNT(bsc.id) AS business_count
FROM
fi_business b
INNER JOIN
fi_business_subcategory bsc ON b.id = bsc.business_id
AND (b.deleted_at IS NULL) AND (bsc.deleted_at IS NULL)
INNER JOIN
fi_suburb su ON su.id = b.suburb_id AND su.city_id = 1
RIGHT JOIN
fi_subcategory sc ON sc.id = bsc.subcategory_id
RIGHT JOIN
fi_category c ON c.id = sc.category_id AND (sc.deleted_at IS NULL)
WHERE
(c.deleted_at IS NULL)
GROUP BY
c.id, sc.id
如果你想使用子查询,正确地表达出你与作为痘痘变化可能第二次查询是这样的:
SELECT
c.id,
c.name,
c.slug,
sc.id,
sc.name,
sc.slug,
IFNULL(bsc.business_count, 0)
-- turn NULL from left join into 0
FROM
fi_category c
LEFT JOIN
fi_subcategory sc ON c.id = sc.category_id AND (sc.deleted_at IS NULL)
LEFT JOIN (
SELECT
COUNT(*) business_count, t1.subcategory_id
-- removed table name from alias name,
-- and improved performance by simply counting rows
FROM
fi_business_subcategory t1
LEFT JOIN
fi_business t2 ON t2.id = t1.business_id
LEFT JOIN
fi_suburb t3 ON t3.id = t2.suburb_id
LEFT JOIN
fi_city t4 ON t4.id = t3.city_id
WHERE
t4.id = 1 AND (t1.deleted_at IS NULL)
-- check deletion in subquery for performance
GROUP BY
t1.subcategory_id
) bsc ON sc.id = bsc.subcategory_id
-- no longer need to check deletion here
WHERE
(c.deleted_at IS NULL)
GROUP BY
c.id, sc.id
小提琴这里 。
尝试这个
select
c.id,
c.name,
count(sc.name) as Count
from fi_category as c
left join fi_subcategory as sc on sc.category_id = c.id
left join fi_business_subcategory as fbs on fbs.subcategory_id = sc.id
inner join (
select
fb.name,
fs.id,
fs.city_id
from fi_business as fb
inner join fi_suburb as fs on fs.id = fb.suburb_id
where fs.city_id = 1
) as fb on fb.id = fbs.business_id
group by c.id