如何在mysql和php

时间:2018-03-09 09:13:41

标签: php mysql

我想知道你是否可以提供帮助。我有一份员工姓名表以及他们的销售额按月计算

Name            |  surname   | local    | Jan   |  Feb    
————————————————+———    
John.           | Bushill    | Exeter   | 18,500 | 20,100    
Richard         | Niels.     | Plymouth | 21,500 | 19,000    
Emily           | winter     | London   | 30,000 | 21,000

我想创建一个sql查询,将表格视为行而不是列,如下所示

Name.            |  Sales tot    
—————————————————+——    
John Bushill.    | 38,600    
Richard Niels.   | 40,500    
Emily winter.    | 51,000

我正在使用mysql,我知道我可以使用案例陈述,但如果月份的数量也可能有所不同,那么月份可能也会有所不同。

很抱歉,如果这是一个小问题,但我对编程很陌生。

提前感谢您的帮助。山姆

3 个答案:

答案 0 :(得分:0)

如果您必须按月使用销售额作为起点,则可以从information_schema.columns构建查询并执行预准备语句。

ariaDB [sandbox]> drop table if exists t;
Query OK, 0 rows affected (0.10 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> create table t (Name            varchar(20),  surname varchar(20),  local varchar(20), Jan int,  Feb int);
Query OK, 0 rows affected (0.22 sec)

MariaDB [sandbox]> insert into t values
    -> ('John.'           , 'Bushill'    , 'Exeter'   , 18500 , 20100),
    -> ('Richard'         , 'Niels.'     , 'Plymouth' , 21500 , 19000) ,
    -> ('Emily'           , 'winter'     , 'London'   , 30000 , 21000)
    -> ;
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> set @sql =(select
    ->  concat('select concat(name,surname), ',
    -> group_concat(concat('ifnull(',column_name,',0)') SEPARATOR '+'),
    -> ' as total from ', table_name ,';'
    -> )
    -> from information_schema.columns
    -> where table_name = 't' and table_schema = 'sandbox' and ordinal_position > 3
    -> order by ordinal_position
    -> );
Query OK, 0 rows affected (0.02 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> prepare sqlstmt from @sql;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

MariaDB [sandbox]> execute sqlstmt;
+----------------------+-------+
| concat(name,surname) | total |
+----------------------+-------+
| John.Bushill         | 38600 |
| RichardNiels.        | 40500 |
| Emilywinter          | 51000 |
+----------------------+-------+
3 rows in set (0.00 sec)

MariaDB [sandbox]> deallocate prepare sqlstmt;
Query OK, 0 rows affected (0.00 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> #Simulate new column added
MariaDB [sandbox]> alter table t
    -> add column mar int;
Query OK, 0 rows affected (0.37 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> update t set mar = 30 where name = 'john.';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [sandbox]> update t set mar = 10 where name = 'richard';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> set @sql =(select
    ->  concat('select concat(name,surname), ',
    -> group_concat(concat('ifnull(',column_name,',0)') SEPARATOR '+'),
    -> ' as total from ', table_name ,';'
    -> )
    -> from information_schema.columns
    -> where table_name = 't' and table_schema = 'sandbox' and ordinal_position > 3
    -> order by ordinal_position
    -> );
Query OK, 0 rows affected (0.02 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> prepare sqlstmt from @sql;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

MariaDB [sandbox]> execute sqlstmt;
+----------------------+-------+
| concat(name,surname) | total |
+----------------------+-------+
| John.Bushill         | 38630 |
| RichardNiels.        | 40510 |
| Emilywinter          | 51000 |
+----------------------+-------+
3 rows in set (0.00 sec)

MariaDB [sandbox]> deallocate prepare sqlstmt;
Query OK, 0 rows affected (0.00 sec)

答案 1 :(得分:-1)

您可以通过calculating /使用MySQL String-functionsSELECT clasue的字段选择中的数据来解决此问题:

SELECT
    CONCAT(table.name, ' ', table.surname) as Name,
    table.Jan + table.Feb as 'Sales lot'
FROM
    table;

答案 2 :(得分:-1)

SELECT GROUP_CONCAT(name,' ',surname) as Name,jan+feb as sales_tot FROM `tbl_monthly` group by name

输出: https://i.stack.imgur.com/Ya7eV.png