大家好,下午好,
我在MySQL中有5个字段,有:
ItemCode
Description
OrderQty
ApprovedQty
UoM
UnitPrice
Total
我无法用正确的词语解释它,但我希望你能通过让我向你展示这一点来理解它。
_______________________________________________________________
|ItemCode|Description|OrderQty|ApprovedQty|UOM|UnitPrice|Total|
---------------------------------------------------------------
|12345678|Ketchup |12.00 |0.00 |PC |1.00 |12.00|
|67891111|Soy Sauce |0.00 |12.00 |PC |1.00 |12.00|
---------------------------------------------------------------
现在你看到了,我希望你现在能理解它。 这是我的问题,我有一个代码,将上面的数据转移到Datagridview,但这是一个扭曲,扭曲是问题。
这是:
如何显示这样的数据;如果ApprovedQty为0.00,那么OrderQty中的数据将显示,但如果ApprovedQty不是0.00,无论OrderQty中的数据是什么,ApprovedQty将显示。
像这样。
基于上表
___________________________________________________
|ItemCode|Description|OrderQty|UOM|UnitPrice|Total|
---------------------------------------------------
|12345678|Ketchup |12.00 |PC |1.00 |12.00|
|67891111|Soy Sauce |12.00 |PC |1.00 |12.00|
---------------------------------------------------
这是我的代码,但它只显示已批准的数量
Private Sub loadfinalpurch1()
Dim con1 As MySqlConnection = New MySqlConnection("server=localhost;userid=root;password=admin1950;database=inventory")
Dim sql1 As MySqlCommand = New MySqlCommand("Select ItemCode,Description,ApprovedQty,UoM,UnitPrice,Total from final_purch where PRnumber = '" & Label2.Text & "' and Added is NULL OR Added ='';", con1)
Dim ds1 As DataSet = New DataSet
Dim adapter1 As MySqlDataAdapter = New MySqlDataAdapter
con1.Open()
adapter1.SelectCommand = sql1
adapter1.Fill(ds1, "MyTable")
DataGridView1.DataSource = ds1.Tables(0)
con1.Close()
With DataGridView1
.RowHeadersVisible = False
.Columns(0).HeaderCell.Value = "Item Code"
.Columns(1).HeaderCell.Value = "Description"
.Columns(2).HeaderCell.Value = "Order Qty"
.Columns(3).HeaderCell.Value = "UOM"
.Columns(4).HeaderCell.Value = "Unit Price"
.Columns(5).HeaderCell.Value = "Total Amount"
End With
DataGridView1.Columns.Item(0).Width = 90
DataGridView1.Columns.Item(1).Width = 200
DataGridView1.Columns.Item(2).Width = 90
DataGridView1.Columns.Item(3).Width = 90
DataGridView1.Columns.Item(4).Width = 100
DataGridView1.Columns.Item(5).Width = 100
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = "Tag"
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
DataGridView1.Columns.Insert(0, checkBoxColumn)
End Sub
我希望你明白我的观点。我和MYSQL命令有什么关系?与我的代码?我不知道如何实现这一点。
TYSM未来帮助
答案 0 :(得分:0)
您无法在任何地方执行此操作,您可以在代码上执行此操作或直接更改查询:
Select ItemCode,
Description,
IF(ApprovedQty = 0, OrderQty, ApprovedQty) as myApprovedQty,
UoM,
UnitPrice,
Total from final_purch where PRnumber = '" & Label2.Text & "' and Added is NULL OR Added ='';", con1
答案 1 :(得分:0)
您应该使用case语句在SQL中实现它。
https://dev.mysql.com/doc/refman/5.7/en/case.html
使用IF()函数的上述答案是正确的,但在我看来,最好使用CASE-WHEN以匹配其他数据库引擎,如Microsoft SQL Server和Oracle。
答案 2 :(得分:-1)
SELECT
ItemCode,
Description,
IF (ApprovedQty = 0, OrderQty, ApprovedQty),
UoM,
UnitPrice,
Total
FROM table