2014-11-24 65 views
0

使用選擇我有表姓名jual我怎樣才能查詢從表在MySQL

Field  | Type  | Null | Key | Default | Extra | 
+------------+-------------+------+-----+---------+-------+ 
| id_jual | int(11)  | NO | PRI | NULL |  | 
| tanggal | date  | YES |  | NULL |  | 
| jumlah  | int(4)  | NO |  | NULL |  | 
| id_obat | int(11)  | YES | MUL | NULL |  | 
| id_periksa | int(11)  | YES | MUL | NULL |  | 
| pemakaian | varchar(30) | YES |  | NULL |  | 

當我執行select * from jual「在這裏結果」

id_jual | tanggal | jumlah | id_obat | id_periksa | pemakaian | 
+---------+------------+--------+---------+------------+-----------+ 
|  1 | 2014-10-29 |  20 |  1 |   1 | 3x1  | 
|  2 | 2014-10-29 |  10 |  5 |   1 | 2x1  | 
|  5 | 2014-10-29 |  20 |  8 |   2 | 2x1  | 
|  6 | 2014-10-29 |  20 |  1 |   3 | 3x1  | 
|  10 | 2014-10-29 |  13 |  5 |   4 | 2x1  | 
|  11 | 2014-10-29 |  10 |  8 |   4 | 2x1  | 
|  12 | 2014-10-29 |  30 |  1 |   5 | 2x1  | 
|  15 | 2014-10-29 |  30 |  3 |   6 | 21  | 
|  16 | 2014-10-29 |  20 |  8 |   6 | 3x1  | 
|  18 | 2014-10-29 |  30 |  3 |   7 | 1x2  | 
|  22 | 2014-10-30 |  20 |  3 |   9 | 1x2  | 
|  39 | 2014-10-30 |  10 |  3 |   18 | 1x1  | 
|  43 | 2014-10-31 |  15 |  3 |   20 | 2x1  | 
|  46 | 2014-10-31 |  10 |  5 |   21 | 2x1  | 
|  47 | 2014-11-01 |  20 |  6 |   22 | 1x1  | 
|  50 | 2014-11-01 |  7 |  6 |   23 | 1x1  | 

我怎樣才能得到這樣的結果視圖這樣的:

id_obat |       tanggal 
+---------+------------+----------+----------+----------+-----------+ 
      2014-10-29 |2014-10-30|2014-10-31|2014-11-01|2014-11-02 
+---------+------------+----------+----------+----------+-----------+ 
| 1  | 20.20.30 |   |   |   |   | 
| 3  | 30.30  | 20.10 | 15  |   |   | 
| 5  | 10.13  |   | 10.  |   |   | 
| 6  |   |   | 20.7  |   |   | 
| 8  | 20.10.20 |   |   |   |   | 
+---------+------------+----------+----------+----------+------------+ 

請幫我與我的問題..... !!!!

+0

你想要的是一個數據透視表,一般需要報告或電子表格軟件,或更高級的數據庫,如MS SQL。這裏是一個類似的問題:http://stackoverflow.com/questions/7674786/mysql-pivot-table – Sheepy 2014-11-24 06:10:13

回答

0

你可以用這樣的動力支點聲明這樣做:

SET @sql = NULL; 
SELECT 
    GROUP_CONCAT(DISTINCT 
    CONCAT(
     'MAX(IF(tanggal = ''', 
     tanggal, 
     ''', val, NULL)) AS ''', 
     tanggal, 
     '''' 
    ) 
) INTO @sql 
FROM jual; 

SET @sql = CONCAT(' 
     SELECT id_obat, ', @sql, ' 
     FROM (
     SELECT 
      tanggal, 
      id_obat, 
      GROUP_CONCAT(jumlah SEPARATOR ''.'') AS val 
     FROM jual 
     GROUP BY tanggal, id_obat 
     ORDER BY id_jual) AS DERIVED_TABLE 
     GROUP BY id_obat;'); 

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

看到這個sample SQL Fiddle

結果不會將您的樣品輸出完全一致,但我認爲這是因爲樣本輸出不正確,因爲值20.7應該在2014-11-01之下,而不是像樣本中那樣2014-10-31。

+0

是的,這是真的,但我如何創建視圖上面的樞軸聲明? – user1191933 2014-11-25 11:08:38

+0

@ user1191933如果你希望它是動態的,我不認爲你可以使用視圖,因爲我記得視圖不能包含動態代碼。 – jpw 2014-11-25 11:23:11

0
SELECT tangaal, id_obat, GROUP_CONCAT(jumlah) AS jumlah 
FROM `jual` 
GROUP BY tangaal, id_obat 

這會給輸出

tangaal  | id_obat  | jumlah  | 
------------+---------------+---------------+ 
2014-10-29 | 1   | 20,30,20 | 
------------+---------------+---------------+ 
2014-10-29 | 3   | 30,30  | 
------------+---------------+---------------+ 
.............. 
+0

感謝您的回覆:-D – user1191933 2014-11-25 11:09:14