如何删除内部连接并使用左连接?
我有两个表,分别是dishinfo和category,
dishinfo有id_cate,这是分类的数量
例如:
id_cate:1
id_cate:1
id_cate:2而category表有:
id_cate:1 == cate_descrip:dessert
id_cate:2 == cate_decrip:lunch我的代码:
SELECT dishinfo.id_cate,cate_descrip
FROM dishinfo
INNER JOIN category
ON dishinfo.id_cate=category.id_cate;发布于 2018-03-27 21:55:16
如果你做一个简单的左连接,它会是这样的:
SELECT dishinfo.id_cate, cate_descrip
FROM dishinfo
LEFT JOIN category
ON dishinfo.id_cate = category.id_cate;但这将返回dishinfo中的所有记录,包括那些具有id_cate NULL的记录。为了防止这种情况发生,您可以这样做:
SELECT dishinfo.id_cate, cate_descrip
FROM dishinfo
LEFT JOIN category ON dishinfo.id_cate = category.id_cate
WHERE dishinfo.id_cate IS NOT NULL;有了额外的条件,您将阻止选择那些没有id_cate的记录
https://stackoverflow.com/questions/49514330
复制相似问题