Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

Display day or date as column in mysql

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

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

1 Reply

0 votes
by (71.8m points)

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..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...