简体   繁体   English

MySQL在基于性别和生日的选择情况下显示空值

[英]MySQL display null value in select case based on gender and birthdate

I have SQL syntax with CASE condition based on birth and gender. 我有基于出生和性别的CASE条件的SQL语法。 Below is the sample query that I use 以下是我使用的示例查询

SELECT 
  Age,
  SUM(gender = 'M') M,
  SUM(gender = 'F') F,
  COUNT(*) TotalPerson 
FROM
  (SELECT 
    CASE
      WHEN FLOOR(DATEDIFF(CURDATE(), birdth) / 365)  60 THEN '60 Above' 
    END Age,
    gender 
  FROM
    society) ageList 
GROUP BY Age

the result become like this: 结果变成这样:

Age         | M     | F     | TotalPerson
--------------------------------
19 - 25     | 1     | 0     | 1
19 - 25     | 1     | 0     | 1
26 - 45     | 1     | 2     | 3
46 - 60     | 0     | 1     | 1
60 Above    | 0     | 2     | 2

for full syntax here is my sqlfiddle: 对于完整的语法,这是我的sqlfiddle: http://sqlfiddle.com/#!9/e8c12a/2 http://sqlfiddle.com/#!9/e8c12a/2

but the result that I really need is the age with null value also display. 但我真正需要的结果是同时显示具有空值的年龄。
example like this: 像这样的例子:

Age         | M     | F     | TotalPerson
--------------------------------
0 - 5       | 0     | 0     | 0
6 - 10      | 0     | 0     | 0
11 - 18     | 0     | 0     | 0
19 - 25     | 1     | 0     | 1
19 - 25     | 1     | 0     | 1
26 - 45     | 1     | 2     | 3
46 - 60     | 0     | 1     | 1
60 Above    | 0     | 2     | 2

You may join ages with society: 您可以与社会同龄:

SELECT 
  Age,
  COALESCE(SUM(gender = 'M'), 0) M,
  COALESCE(SUM(gender = 'F'), 0) F,
  COALESCE(SUM(gender = 'M'), 0) + COALESCE(SUM(gender = 'F'), 0) TotalPerson 
FROM
  (
    SELECT '0 - 5' AS Age, 0 AS age1, 6 AS age2 UNION
    SELECT '6 - 10', 6, 10 UNION
    SELECT '11 - 18', 11, 18 UNION
    SELECT '19 - 25', 19, 25 UNION
    SELECT '26 - 45', 26, 45 UNION
    SELECT '46 - 60', 46, 60 UNION
    SELECT '60 Above', 60, 1000
  ) ages
  LEFT JOIN society ON FLOOR(DATEDIFF(CURDATE(), birdth) / 365) BETWEEN age1 AND age2
GROUP BY Age
ORDER BY age1

Example on SQL Fiddle SQL小提琴示例

There are several things to consider here. 这里有几件事情要考虑。

First of all, you should think really hard if you have to do this with SQL. 首先 ,如果必须使用SQL进行操作,则应该认真思考。 Imho, the query should only responsible for getting the data from the database, and not - in case you do so - that the display of all age groups in a GUI or something like that is complete. 恕我直言,查询只应负责从数据库中获取数据,而不是(如果您这样做的话)负责GUI中所有年龄段的显示或类似操作。

Secondly , the reason that your approach is not working is the way how the query is processed. 其次 ,您的方法不起作用的原因是查询的处理方式。 Simplified in my own words, what MySQL does is this: 用我自己的话来简化,MySQL的作用是:

  1. Get all rows from society table in subquery 从子查询的社会表中获取所有行
  2. Execute main query with the result of subquery 使用子查询的结果执行主查询

Thirdly , and last but not least. 第三 ,最后但并非最不重要。 If you really HAVE TO do it in MySQL, here would be a doable approach. 如果您确实必须在MySQL中执行此操作,那么这将是一个可行的方法。 Create a table with all the age groups you want to have (can also be a temporary table), and then join in the right way (in my example with an RIGHT join, but I think one could also turn the query around and do a LEFT join, you can only not use a INNER JOIN as this negates to possibility to have rows in the final result set that are NULL on one side of the join), so you also get your NULL results. 创建一个包含所有年龄组的表(也可以是临时表),然后以正确的方式进行联接(在我的示例中为RIGHT联接,但我认为也可以扭转查询的位置并进行LEFT联接,您只能使用INNER JOIN,因为这样会否定最终结果集中的行在联接的一侧为NULL的可能性,因此您也将获得NULL结果。 I created a simple example on sqlfiddle based on your approach. 我根据您的方法在sqlfiddle上创建了一个简单的示例 Please note however, that it is only a quick and dirty solution (eg I cheated on the max date range by setting to age_to to 1000). 但是请注意,这只是一个快速而肮脏的解决方案(例如,我通过将age_to设置为1000欺骗了最大日期范围)。

On a side note: I would recommend to not calculate the age by dividing the days by 365. Remember that there are leap years, so over time your result will become more and more inaccurate. 附带说明:我不建议将天数除以365来计算年龄。请记住,存在leap年,因此随着时间的推移,您的结果将越来越不准确。 Easiest (and most reliable way) for this is imho to subtract the birth year from the current year and check if the date already passed this year, in order to subtract 1 from the result if it did not. 最简单(也是最可靠的方法)是imho从当年减去出生年份,并检查日期是否已经过今年,以便从结果中减去1。

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

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