我的数据库中有以下数据
插入数据时我有一些重复的记录。
CREATE TABLE `historical_data` (
`symbol_name` varchar(70) DEFAULT NULL,
`current_day` varchar(50) DEFAULT NULL,
`open_val` varchar(50)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
Insert Into historical_data values('SBIN','10-DEC-2015','300.10');
Insert Into historical_data values('SBIN','10-DEC-2015','300.10');
Insert Into historical_data values('SBIN','10-DEC-2015','300.10');
Insert Into historical_data values('ACC','10-DEC-2015','1233.10');
Insert Into historical_data values('TATELXSI','10-DEC-2015','1980.10');
您能告诉我如何从表中删除所有重复项吗
重复表示相同的符号和当天相同
答案 0 :(得分:4)
没有必要这样做。只需按原样运行此查询: -
ALTER IGNORE TABLE `historical_data`
ADD UNIQUE INDEX (`symbol_name`, `current_day`, `open_val` );
这将为您的表添加唯一索引并删除所有正常的行,并确保将来不会插入重复的行。
答案 1 :(得分:0)
您可以使用STORED PROCEDURE
记下CURSOR
以读取每一行,并运行循环以查找任何类似的行,如果找到,您只需要DELETE
该特定行行。
答案 2 :(得分:0)
您可以向表中添加主键,以便可以在不使用游标的情况下区分重复项,然后通过将表连接到自身来删除它们:
Sub Button2_Click()
Dim i As Integer, TotalEffortEC As Double, TotalNCEC As Double, TotalSizeEC As Double
Dim TotalEffortWB As Double, TotalNCWB As Double, TotalSizeWB As Double
TotalEffortEC = 0
TotalNCEC = 0
TotalSizeEC = 0
TotalEffortWB = 0
TotalNCWB = 0
TotalSizeWB = 0
i = 3
With Worksheets("Valid Data")
Do While .Cells(i, 6).Value <> ""
If .Cells(i, 7).Value = "WB" Then
TotalEffortWB = .Cells(i, 23).Value + TotalEffortWB
TotalNCWB = .Cells(i, 14).Value + TotalNCWB
TotalSizeWB = .Cells(i, 13).Value + TotalSizeWB
Else
TotalEffortEC = .Cells(i, 23).Value + TotalEffortEC
TotalNCEC = .Cells(i, 14).Value + TotalNCEC
TotalSizeEC = .Cells(i, 13).Value + TotalSizeEC
End If
i = i + 1
Loop
End With
With Worksheets("Summary")
.Range("A2").Value = (TotalEffortEC + .Range("B10") + .Range("C10")) / TotalNCEC
.Range("B2").Value = (TotalEffortEC + .Range("B10") + .Range("C10")) / TotalSizeEC
.Range("A3").Value = (TotalEffortWB + .Range("B11") + .Range("C11")) / TotalNCWB
.Range("B3").Value = (TotalEffortWB + .Range("B11") + .Range("C11")) / TotalSizeWB
End With
End Sub
或使用唯一索引:
Sub test()
Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
Dim cl As Range, i&, k, x%, z%
Dic.comparemode = vbTextCompare
i = [B:B].Find("*", , xlValues, , xlByRows, xlPrevious).Row
For Each cl In Range([B2], Cells(i, "B"))
If Not Dic.exists(cl.Value2) Then
Dic.Add cl.Value2, "1|" & cl.Offset(, -1).Value2
Else
x = Split(Dic(cl.Value2), "|")(0)
z = x + 1
Dic(cl.Value2) = Replace(Dic(cl.Value2), x, z) & _
"|" & cl.Offset(, -1).Value2
End If
Next cl
Workbooks.Add
[A1] = "Room": [B1] = "Count": i = 2
For Each k In Dic
Cells(i, "A") = k
Range(Cells(i, 2), Cells(i, Split(Dic(k), "|")(0) + 2)) = Split(Dic(k), "|")
i = i + 1
Next k
End Sub