English 中文(简体)
SlectT公司是MySQL的一栏?
原标题:SELECT rows as columns in MySQL?

审议以下表格“概览”

user_id  _date       cnt
------------------------
1        2011-02-10  123
1        2011-02-11   99
1        2011-02-12  100
1        2011-02 13   12
2        2011-02-10   32
2        2011-02-11  433
2        2011-02-12  222
2        2011-02 13  334
3        2011-02-10  766
3        2011-02-11  654
3        2011-02-12   43
3        2011-02 13   27
...
100      2011-02-13  235

如你所知,该表每天对每个用户(用户_id)进行页面访问。 I m 寻找能够输出用户的电离层电离层电离层电离层电离层电离层电离层电离层的电离层电离层电离层电离层电离层电离层电离层电离层。

_date         1    2    3 ... 100
---------------------------------
2011-02-10  123   32  766
2011-02-11   99  433  654
2011-02-12  100  222   43
2011-02-13   12  334   27     235

这是否与选任考试委员会的声明有关?

最佳回答

如果你重新处理一套有限的用户识别器,你可以这样做:

SELECT _date,
    SUM(CASE WHEN _user_id = 1 THEN cnt ELSE 0 END) AS user1,
    SUM(CASE WHEN _user_id = 2 THEN cnt ELSE 0 END) AS user2,
    SUM(CASE WHEN _user_id = 3 THEN cnt ELSE 0 END) AS user3,
    ...
FROM views
GROUP BY _date

然而,它比一个好奇多。

问题回答

它期望你们有你们想要改变的长串价值观。 如果是这样的话,你可以使用。 你的法典将照此办理(见SQL Fiddle with Demo):

CREATE TABLE Table1
    (`user_id` int, `_date` datetime, `cnt` int)
;

INSERT INTO Table1
    (`user_id`, `_date`, `cnt`)
VALUES
    (1,  2011-02-09 17:00:00 , 123),
    (1,  2011-02-10 17:00:00 , 99),
    (1,  2011-02-11 17:00:00 , 100),
    (1,  2011-02-13 00:00:00 , 12),
    (2,  2011-02-09 17:00:00 , 32),
    (2,  2011-02-10 17:00:00 , 433),
    (2,  2011-02-11 17:00:00 , 222),
    (2,  2011-02-13 00:00:00 , 334),
    (3,  2011-02-09 17:00:00 , 766),
    (3,  2011-02-10 17:00:00 , 654),
    (3,  2011-02-11 17:00:00 , 43),
    (3,  2011-02-13 00:00:00 , 27),
    (100,  2011-02-12 17:00:00 , 235)
;

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
       sum(case when user_id =    ,
      user_id,
           then cnt else 0 end) AS    ,
      user_id,     
    )
  ) INTO @sql
FROM Table1;

SET @sql = CONCAT( SELECT _Date,  , @sql,   
                  FROM table1 
                  GROUP BY _Date );

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签