这个mysql代码的小解释 [英] Little explanation of this mysql code

查看:48
本文介绍了这个mysql代码的小解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前在另一个问题上从某人那里得到了这个答案,但我想得到一个解释,以便我完全理解这个问题.

I got this answer from somebody on another Q before, but I would like to get an explanation of this, so that I fully understand the problem.

我有一个表格,其中有一列包含私人"或公司"的值.问题是,我想检查我的记录中有多少私人"和公司"值,以便我稍后可以将它们与搜索结果一起显示.因此,如果有 4 个公司广告和 1 个私人广告,结果如下:

I have a table, with a column which contains a value of either "private" or "company". Thing is, I want to check how many "private" and "company" values are in my records, so that I can display them with the search results later on. So if there are 4 company ads, and 1 private ad, this is the results:

  Company = 4
  Private = 1
  All ads = 5

这是我从某人那里得到的关于如何做到这一点的代码,换句话说,这就是我想要解释的:

And here is the code I got from someone about how to do this, in other words, this is what I would like explained:

SELECT
    IFNULL( field , 'All ads' ) AS 'Type',
    COUNT( * )
FROM
    `table`
GROUP BY
    field
WITH ROLLUP

谢谢

推荐答案

选择Type字段,如果值为null,则使用All Ads作为默认值.

Select the Type field, and if the value is null, use All Ads as the default value.

table 中选择它们,并按字段对它们进行分组.

Select them from the table, and group them by field.

据我所知,这将计算每个类型"的条目数,所有具有空值的条目将直接归入所有广告"计数.

As far as I can tell, this will count each the number of entries of each 'Type', and all the entries that have a null value will simply go under the 'All Ads' count.

顺便说一句,如果您查看 IFNULL() 函数,很容易弄清楚.它清楚地说明了IF NULL,因此它正在检查某些内容是否为空.然后向它传递一个字段名称和一个静态值.它正在检查该字段值是否为假是有道理的.唯一的另一件事是值,因此我们可以得出结论,如果字段值为空,它将使用静态值.

Just BTW, if you look at the IFNULL() function, it is pretty easy to figure out. It clearly states IF NULL, so it is checking if something is null. Then you pass it a field name and a static value. Kind of makes sense that it is checking if that field value is false. And the only other thing there is the value, so we can come to the conclusion that it will use the static value, if the field value is null.

这篇关于这个mysql代码的小解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆