简体   繁体   English

MySQL的左联接不返回空行

[英]mysql left join not returning empty row

I'm trying to get the list of categories with number of child records present in there. 我正在尝试获取其中存在子记录数量的类别列表。 If the categories doesn't have records it should return NULL or 0 but my query returning categories with child records looks like its skipping the one without child records. 如果类别没有记录,则它应该返回NULL或0,但是我的查询返回带有子记录的类别看起来像是跳过没有子记录的类别。 ... will really appreciate the help. ……将非常感谢您的帮助。

here's my code: 这是我的代码:

SELECT 
t_gal.f_sub_category_id, 
t_sub_cat.f_sub_cat_name, 
t_gal.f_image_thumb, (
    SELECT COUNT(*)
    FROM t_gallery
    WHERE f_sub_category_id = t_gal.f_sub_category_id) 
    AS f_image_total
FROM    t_gallery t_gal
LEFT JOIN t_sub_category t_sub_cat ON t_sub_cat.r_id = t_gal.f_sub_category_id
GROUP BY t_sub_cat.r_id
ORDER BY t_gal.f_added_on DESC, t_gal.r_id DESC

Here's the two tables: 这是两个表:

在此处输入图片说明

在此处输入图片说明

Problem appears to be your group by clause. 问题似乎是您的group by子句。

You are grouping by a field that is on the LEFT JOINed table, hence when it does the group by all the rows which do not have a matching row on that table would appear to be aggregated into a single row. 您正在按LEFT JOINed表上的一个字段进行分组,因此,当按该表上没有匹配行的所有行进行分组时,该行似乎会聚合为单个行。

I think what you are trying to get is a list of gallery items, along with the category they are in (if found) and the count of other galleries in the same category. 我认为您要获取的是画廊项目的列表,以及它们所在的类别(如果找到)以及同一类别中其他画廊的数量。 If so try the following (if not let me know!) 如果是这样,请尝试以下操作(如果不告诉我!)

SELECT t_gal.f_sub_category_id, t_sub_cat.f_sub_cat_name, t_gal.f_image_thumb, Sub1.GalleryCount
FROM    t_gallery t_gal
LEFT JOIN t_sub_category t_sub_cat 
ON t_sub_cat.r_id = t_gal.f_sub_category_id
LEFT OUTER JOIN (SELECT f_sub_category_id, COUNT(*) AS GalleryCount FROM t_gallery GROUP BY f_sub_category_id) Sub1
ON Sub1.f_sub_category_id = t_gal.f_sub_category_id
ORDER BY t_gal.f_added_on DESC, t_gal.r_id DESC

It LOOKS like for every sub-category (of a previously selected category), you want to include ALL of that sub-category... And, of those sub-categories, you want a count of how many images for that category wheather or not there even IS an image in the gallery table. 就像每个子类别(先前选择的类别)一样,您希望包括该子类别的所有...。在这些子类别中,您希望统计该类别的图像数目或画廊表中甚至没有图像。

What you may have been running into is the select statement for the FIELD used to count images... first, that could become a performance killer. 您可能遇到的是用于对图像进行计数的FIELD的select语句...首先,它可能成为性能杀手。 Instead, you could just do a left-join directly to the gallery table and COUNT the distinct R_IDs from the gallery FOR the corresponding sub-category 相反,您可以直接对图库表进行左联接,并为相应的子类别计算图库中不同的R_ID

SELECT 
      t_sub_cat.r_id,
      t_sub_cat.f_sub_cat_name, 
      MIN( COALESCE( t_gal.f_image_thumb, '' )) as JustOneThumbImg, 
      COUNT( DISTINCT( t_gal.r_id )) SubCategoryImageCount
   FROM 
      t_sub_category t_sub_cat 
         LEFT JOIN t_gallery t_gal
            ON t_sub_cat.r_id = t_gal.f_sub_category_id
   GROUP BY 
      t_sub_cat.r_id
   ORDER BY 
      t_sub_cat.f_added_on DESC

Since you are not grabbing all gallery images (since some may not exist FOR a given sub-category), ordering by the t_gal.r_id doesn't make sense 由于您没有获取所有图库图像(因为对于给定的子类别可能不存在某些图像),因此按t_gal.r_id进行排序是没有意义的

Also, the reason I'm not pre-grabbing aggregates in a sub-query to join against... I don't want to get everything from every category / sub-category without knowing which sub-categories are associated with the category you actually want. 另外,我之所以没有在子查询中预先获取汇总以加入……的原因是我不想从每个类别/子类别中获取所有信息,而又不知道哪些子类别与您所关联的类别相关联真正想要的。

the problem with your query is that you are using t_gallery as your main table and not t_sub_category while using left join. 查询的问题是您在使用t_gallery作为主表,而不是在使用左连接时使用t_sub_category。

you could try this: sqlfiddle 你可以试试这个: sqlfiddle

select 
t_gal.f_sub_category_id, 
t_sub_cat.f_sub_cat_name, 
 (
    SELECT COUNT(*)
    FROM t_gallery
    WHERE f_sub_category_id = t_gal.f_sub_category_id) 
    AS f_image_total
from t_sub_category as t_sub_cat
left join t_gallery t_gal on t_gal.f_sub_category_id = t_sub_cat.r_id
GROUP BY t_sub_cat.r_id
ORDER BY t_gal.r_id DESC;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM