Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is it possible to display day from a query that have parameter date1 and date2.

for example :

SELECT e.struk_no, DATE(e.created_at), c.id,  
                                count(a.harga_jual) as counting,
                                sum(a.harga_jual) AS total_item
                    FROM transaction_detail a

                    LEFT JOIN `transaction` e
                    ON a.transaction_id = e.id

                    LEFT JOIN karyawan b
                    ON a.karyawan_id = b.id

                    LEFT JOIN list_harga c
                    ON a.item_id = c.id

                    LEFT JOIN item_layanan d
                    ON c.item_layanan_id = d.id

                    WHERE DATE(e.created_at) BETWEEN DATE('2016-11-13') AND DATE('2016-11-15')

                    GROUP BY  a.karyawan_id, b.nama_karyawan, d.nama_item

I got this :

+----------+--------------------+----+----------+------------+
| struk_no | DATE(e.created_at) | id | counting | total_item |
+----------+--------------------+----+----------+------------+
|        2 | 2016-11-15         |  5 |        1 | 90000      |
|        1 | 2016-11-14         |  4 |        2 | 200000     |
|        4 | 2016-11-15         | 16 |        1 | 400000     |
|        3 | 2016-11-15         | 19 |        1 | 75000      |
+----------+--------------------+----+----------+------------+
4 rows in set

Please see : WHERE DATE(e.created_at) BETWEEN DATE('2016-11-13') AND DATE('2016-11-15')

Is is possible to create like this

+----------+----+----+----+----+----------+------------+
| struk_no | 13 | 14 | 15 | id | counting | total_item |
+----------+----+----+----+----+----------+------------+
|        2 |    |    | 1  |  5 |        1 | 90000      |
|        1 |    |  1 | 1  |  4 |        2 | 200000     |
|        4 |    |    | 1  | 16 |        1 | 400000     |
|        3 |    |    | 1  | 19 |        1 | 75000      |
+----------+----+----+----+----+----------+------------+
4 rows in set

The unique parameter is id, What named case like this?

Thanks all

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
171 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this

SELECT e.struk_no,
IF(DATE(e.created_at)= str_to_date("2016-11-13","%Y-%m-%d"),1,0) as '13'
IF(DATE(e.created_at)= str_to_date("2016-11-14","%Y-%m-%d"),1,0) as '14',
IF(DATE(e.created_at)= str_to_date("2016-11-15","%Y-%m-%d"),1,0) as '15',
c.id,
count(a.harga_jual) as counting,
sum(a.harga_jual) AS total_item
                                    FROM transaction_detail a ............

Hope this works..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...