DELETE INNER JOIN使用LEFT JOIN PHP

时间:2018-03-27 13:32:55

标签: php mysql left-join inner-join

如何删除内连接并改为使用左连接?

我有两个表是dishinfo和category,

dishinfo有id_cate,这是类别的数量

例如:

id_cate:1
id_cate:1
id_cate:2

而类别表有:

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;

1 个答案:

答案 0 :(得分:0)

如果你做一个简单的左连接,它将是:

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

的记录