【问题标题】:How to find the nearest value in the database如何在数据库中找到最接近的值
【发布时间】:2015-06-15 09:37:24
【问题描述】:

如何在数据库中查找最接近的值,例如数据库中的数据为:

要测试的数据是-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

【问题讨论】:

  • 你的问题不是很清楚。请描述您的数据库表,并为您的 rdbms 使用适当的标签。

标签: vb.net distance closest euclidean-distance


【解决方案1】:

我的答案是通过使用查询来查找与特定字段最接近的字段值:

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 for @specificValue = -100


作为替代查询(在 MySQL 中):

SELECT *
FROM aTable
ORDER BY
    ABS(-100 - pixel_putih)
LIMIT 1;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2022-01-14
    • 1970-01-01
    • 2012-05-15
    相关资源
    最近更新 更多