如何在数据库中查找最近的值,例如数据库中的值为:
将要测试的数据为-100
,系统如何发出测试结果-100
是字符A
。
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Try
Dim persen As String
Dim nilai_banding, hasil As String
Dim cek = TextBox1.Text
Dim reng_atas = cek + 15
Dim reng_bawah = cek - 15
Dim per = " %"
Dim kuadrat = "^2"
Dim pixeluji = TextBox2.Text
Dim pixelsampel = TextBox1.Text
'Dim C = TextBox3.Text
'If C > 315 Then
'MessageBox.Show("Jumlah Pixel Terlalu Besar Untuk Di Verifikasi", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information)
'Else
'conn.Open()
Dim mDA As New MySql.Data.MySqlClient.MySqlDataAdapter("SELECT citra_karakter From karakter_plat WHERE pixel_putih='" & TextBox1.Text & "' or pixel_putih >='" & reng_atas & "' and pixel_putih >='" & reng_bawah & "'", conn)
'("SELECT Nama,Tanda_Tangan,Nilai_Hitam,Jumlah_Pixel,Keterangan From tanda_tangan WHERE nama LIKE '%" & TextBox7.Text & "%'", conn)
Dim dt As New DataTable
mDA.Fill(dt)
'DataGridView1.DataSource = DataGridViewAutoSizeColumnMode.AllCells
'DataGridView1.DataSource = dt
CMD = New MySql.Data.MySqlClient.MySqlCommand("SELECT nama_huruf,pixel_putih,pixel_hitam,jumlah_pixel,biner_karakter, citra_karakter FROM karakter_plat WHERE pixel_putih='" & TextBox1.Text & "' or pixel_putih<='" & reng_atas & "' and pixel_putih>='" & reng_bawah & "'", conn)
RD = CMD.ExecuteReader()
RD.Read()
If RD.HasRows Then
Label1.Text = RD.Item(0)
TextBox2.Text = RD.Item(1)
'Dim foto As Byte() = RD.Item(5)
End If
上面的语法产生输出-100
= N
我想要-100
= A
。
答案 0 :(得分:0)
我的回答是基于使用查询找到特定字段的字段的最近值:
SELECT *
FROM (
SELECT
*,
ROW_NUMBER() OVER (ORDER BY ABS(@specificValue - valueField)) AS seq
FROM
aTable) t
WHERE
(seq <= 1); --if you want to have n nearest values change `1` to `n`
使用此类型的查询(例如)会为-99
提供-102
而不是@specificValue = -100
。
作为替代查询(在MySQL中):
SELECT *
FROM aTable
ORDER BY
ABS(-100 - pixel_putih)
LIMIT 1;