gpt4 book ai didi

c# - MySQL Select 查询 - 根据日期对数据进行排序

转载 作者:行者123 更新时间:2023-11-29 14:37:11 26 4
gpt4 key购买 nike

我正在使用 MySQL。这是表名称 item_supplier

supplier_ID   Item_ID   Date                  Price    QTY
1 1 2012-01-01 00:00:00 500.00 2
1 1 2012-01-03 00:00:00 450.00 10
2 1 2012-01-01 00:00:00 400.00 5
3 1 2012-05-01 00:00:00 500.00 1

我需要一个显示类似这样的表格的选择查询。

supplier_ID      2012-01-01   2012-01-03   2012-05-01   
1 500.00(2) 450.00(10) null
2 400.00(5) null null
3 null null 500.00(1)

或者至少,

supplier_ID      2012-01-01   2012-01-03   2012-05-01   
1 500.00 450.00 null
2 400.00 null null
3 null null 500.00

我希望有人可以帮助我或给我提示。

最佳答案

如果事先已知的日期数量不是有限的,那么您无法单独在 MySQL 中执行您想要的操作。

你最好的选择是得到一个像这样的表格:

+---------+------------+-------------+-------------+
| Item_ID | Date | supplier_ID | price |
+---------+------------+-------------+-------------+
| 1 | 2012-01-01 | 1 | 500.00 (2) |
| 1 | 2012-01-01 | 2 | 400.00 (5) |
| 1 | 2012-01-03 | 1 | 450.00 (10) |
| 1 | 2012-05-01 | 3 | 500.00 (1) |
| ... | ... | ... | ..... |

可以通过以下方式完成:

SELECT Item_ID,Date,supplier_ID,CONCAT(FORMAT(Price,2),' (',QTY,')') AS price 
FROM item_supplier
ORDER BY Item_ID,Date,supplier_ID;

然后在 C# 端,循环结果并打印所需的输出。由于输出现在按 Item_ID、Date、supplier_ID 排序,因此可以轻松循环结果,然后以所需的格式输出。

关于c# - MySQL Select 查询 - 根据日期对数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8790240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com