MySQL将多行的选定列合并为一个

时间:2015-09-09 09:49:44

标签: mysql rows

我想算出男性和女性的数量。每个年龄的每个宗教的女性用户获得如下表第三个表格的结果表。

我有两张表格如下:

tbl_user

+----+----------+------------+--------+-----------+
| id |   name   |    dob     | gender | religion  |
+----+----------+------------+--------+-----------+
|  1 | raj      | 1999-12-21 | m      |         1 |
|  7 | raju     | 1998-10-10 | m      |         2 |
|  8 | rajan    | 2000-11-23 | m      |         3 |
| 11 | neetu    | 1992-12-06 | f      |         1 |
| 12 | sita     | 1993-06-16 | f      |         2 |
| 13 | rita     | 1992-06-08 | f      |         3 |
| 14 | jenny    | 1993-05-10 | f      |         2 |
| 15 | manju    | 1993-12-16 | f      |         1 |
| 16 | aanju    | 1993-03-05 | f      |         3 |
| 17 | raja     | 1995-04-06 | m      |         1 |
| 18 | rajendra | 1995-07-03 | m      |         2 |
| 19 | rajesh   | 1991-05-02 | m      |         3 |
+----+----------+------------+--------+-----------+

tbl_religion

+----+-----------+
| id |   name    |
+----+-----------+
|  1 | Christian |
|  2 | Hindu     |
|  3 | Islam     |
+----+-----------+

宗教表可以有任意数量的记录(宗教)。

现在我要计算男性和女性的数量。每个年龄的每个宗教的女性用户获得如下所示的结果表。用户可以是任何年龄或任何一年出生的人:

+-----+----------------+------------------+------------+--------------+------------+--------------+
| Age | Christian Male | Christian Female | Hindu Male | Hindu Female | Islam Male | Islam Female |
+-----+----------------+------------------+------------+--------------+------------+--------------+
|  14 |              0 |                0 |          0 |            0 |          1 |            0 |
|  15 |              1 |                0 |          0 |            0 |          0 |            0 |
|  16 |              0 |                0 |          1 |            0 |          0 |            0 |
|  20 |              1 |                0 |          1 |            0 |          0 |            0 |
|  21 |              0 |                1 |          0 |            0 |          0 |            0 |
|  22 |              0 |                1 |          0 |            2 |          0 |            1 |
|  23 |              0 |                0 |          0 |            0 |          1 |            1 |
|  24 |              0 |                0 |          0 |            0 |          0 |            0 |
+-----+----------------+------------------+------------+--------------+------------+--------------+

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

my answer to your previous question的基础上,我们可以使用this q/a中描述的方法使查询动态化,以便处理未知数量的宗教。

最终查询将如下所示:

keyW = txtKeyword.getText();
            search = textField.getText();
            System.out.println("String for car = " + search); 
            System.out.println("String keyword = " + keyW); 



        WebDriver driver = new HtmlUnitDriver();
        ((HtmlUnitDriver)driver).setJavascriptEnabled(true);

        driver.get("https://en.wikipedia.org/wiki/" + search);
        tstr1 = driver.findElement(By.xpath("//*[@id='content']")).getText();

        //JOptionPane.showMessageDialog(null,"Please wait......");

        //driver.close();
        driver.quit();

        String [] items = keyW.split(","); 
        String [] itemsFromArray = new String[items.length];
        for ( int i = 0; i <  items.length; i++)
        {
            itemsFromArray[i] = items[i].trim();
        }
            for(String string : itemsFromArray)
        {       
            //if (args[i].toLowerCase().startsWith(­ "from:" )) 

            System.out.println("FOREACH " + string);
            int i = countWords(tstr1, string);
            System.out.println("Word count "+ string + ":  " + i);
            Keyword1 = ("Count for word " + string + " are " + i);
            listKeys.add(Keyword1);
        }

    }   

Sample SQL Fiddle

答案 1 :(得分:-1)

对于前两个表

<div tabset>
    <div tab ng-repeat="item in teamController.range track by $index">
        <div tab-heading>
            <div class="selected-colour" ng-class="{ 'no-colour-selected': !controller.kit['colour' + ($index + 1)] }" ng-style="{ 'background-color' : '#' + controller.kit['colour' + ($index + 1)] }"></div> {{ controller.kit['colour' + ($index + 1)] }}
        </div>

        {{ controller.kit['colour' + ($index + 1)] === 'ff0000' }}

        <div class="picker colour-picker">
            <ul class="picker-dropdown list-inline">
                <li ng-repeat="colour in teamController.colours" ng-class="{ 'active': controller.kit['colour' + ($index + 1)] === colour.hex }">
                    <a href style="background-color: #{{ colour.hex }};" ng-click="teamController.setColour('colour' + ($parent.$index + 1), colour)"></a>
                </li>
            </ul>
        </div>
    </div>
</div>

表示第三个表格

select count(a.id) as 'Population',
       timestampdiff(year,a.dob,current_date)as 'Age',
       b.name as 'Religion'
from   tbl_user a
inner join tbl_religion b on a.ethnicity=b.name
group by
timestampdiff(year,a.dob,current_date),
       b.name