如何让用户在mysql中的不同年龄组

时间:2018-03-09 06:58:25

标签: mysql activerecord

我有一个表tb_user,其中的记录如下:

Name      gender   location  grewup     date of birth

Arun       male     india    britain    1998-02-03
sonia     female     USA     dubai      1994-02-03
sheetal   female    india    Russia     1993-02-03

我想要的是以下列方式记录:

Age Bracket     Total Count     Female     Male
18-25               50            22        28
26-30               30            12        18
31-36               20            10        10
37-42               10            3          7
43-48               5             2          3

我将查询用作:

select COUNT(id) as total count sum(IF(`sex` = 'Female',1,0)) as female,sum(IF(`sex` = 'Male',1,0)) as male as male FROM tb_user_info GROUP BY `date of birth`

但是我无法像我提到的那样获得年龄段

1 个答案:

答案 0 :(得分:1)

试试这个:

Dim conStr As String = ""

conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes'"
conStr = String.Format(conStr, Path)
Dim connExcel As New OleDbConnection(conStr)
Dim cmdExcel As New OleDbCommand()
Dim oda As New OleDbDataAdapter()
cmdExcel.Connection = connExcel
'Get the name of First Sheet
connExcel.Open()
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim SheetName As String = ""
If dtExcelSchema.Rows.Count > 0 Then
    SheetName = dtExcelSchema.Rows(dtExcelSchema.Rows.Count - 1)("TABLE_NAME").ToString()
End If
connExcel.Close()
'Read Data from First Sheet
connExcel.Open()
cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
oda.SelectCommand = cmdExcel
oda.Fill(dt)
dt.TableName = SheetName.ToString().Replace("$", "")
connExcel.Close()
Return dt

有用的链接:

Get difference in years between two dates in MySQL as an integer

MySQL CASE Expression

MySQL IF Function

MySQL GROUP BY

MySQL ORDER BY: Sort a Result Set