我有一个简单的关系,其中类别和子类别通过parent_id进行区分,类别的parent_id为0,子类别的parent_id为category_id。我想要做的是选择第一个类别的第一个子类别。
为此我正在使用
SELECT cat_id FROM member_product_cat WHERE cat_parent_id !=0 HAVING cat_parent_id=min(cat_parent_id) LIMIT 1
但它给了我一个错误。我知道我可以这样做
SELECT cat_id FROM member_product_cat WHERE cat_parent_id=(Select min(cat_parent_id) from member_product_cat where cat_parent_id !=0) LIMIT 1
但是我的第一种方法有什么问题?我们不是为此有having子句吗?
问候 希曼舒·夏尔马
最佳答案
SQL 中添加了 HAVING 子句,因为 WHERE 关键字不能与聚合函数一起使用。所以你根本不需要在那里。
关于php - 选择最小 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8377027/