[英]mysql select total which don't exist
I am try to get a count of all missing translations and getting really lost in how to do it. 我试图弄清所有缺少的翻译内容,并在执行过程中迷失了方向。
Tables: 表格:
The admin are getting very lazy and I want to be able to show them a count of how many translations are missing. 管理员变得非常懒惰,我希望能够向他们显示缺少多少翻译的计数。
I guess a very simple was of doing this would be to just get the total (languages->count * products->count) but I wanted to return a count for each product separately. 我想这样做很简单,就是只获取总数(语言->数量*产品->数量),但是我想分别返回每个产品的数量。
To do such a query, start with a driver
table (subquery) that has all combinations. 要进行这样的查询,请从具有所有组合的
driver
表(子查询)开始。 Then remove the ones that have translations: 然后删除具有翻译的内容:
select driver.*
from (select distinct l.language_id, p.product_id
from languages l cross join
products p
) driver left outer join
translations t
on t.language_id = driver.language_id and
t.product_id = driver.product_id
where t.language_id is null;
This uses a left outer join
, which keeps everything in the driver
table. 这使用
left outer join
,将所有内容保留在driver
表中。 If there is no match, then the columns in translations
will be NULL
-- the where
clause keeps only these. 如果没有匹配项,则
translations
的列将为NULL
- where
子句仅保留这些内容。
The distinct
may not be necessary in the subquery, if the values in each table are unique. 如果每个表中的值都是唯一的,则在子查询中可能不需要使用
distinct
。
As a note: the above is my preferred way to write the query, because I think it is the clearest in intent. 注意:以上是我编写查询的首选方式,因为我认为这是最明确的意图。 But, MySQL actually materializes the subquery.
但是,MySQL实际上实现了子查询。 So the following is more efficient, if the columns are unique in the two reference tables:
因此,如果两个参考表中的列都是唯一的,则下面的方法更有效:
select l.*, p.*
from languages l cross join
products p left outer join
translations t
on t.language_id = l.language_id and
t.product_id = p.product_id
where t.language_id is null;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.