显示"相似"或"相关"车辆

时间:2014-08-04 14:38:53

标签: sql asp-classic

我有一个车辆数据库,并希望显示"相关"当用户点击查看车辆时的车辆。例如,用户观看2013年雪佛兰Tahoe'这是由经销商和ABC经销商列出的。

我需要创建一个SQL语句来抓取4辆类似于他们正在查看的车辆的车辆。以下是现在的重要性顺序:

1)经销商列表(d_id)(显示该经销商列出的车辆) 2)车辆类别(vc_id)(车辆类别,如汽车,卡车,SUV等) 3)车辆制造(vm_id)(车辆制造如福特,雪佛兰,雷克萨斯等)

我已经创建了一个SQL语句,但它似乎没有做我想要它做的事情。任何人都可以提供有关如何正确构建SQL语句以按上面定义的顺序获取最相关记录的任何建议吗?

strSQL = "SELECT TOP 4 v.v_id, vm.vm_name, v.v_year, v.v_model, v.v_search_price, d.d_name, u.u_name " & _
    "FROM tbl_Vehicles v " & _
    "LEFT JOIN tbl_VehicleMake vm ON vm.vm_id = v.vm_id " & _
    "LEFT JOIN tbl_Dealers d ON d.d_id = v.d_id " & _
    "LEFT JOIN tbl_Users u ON u.u_id = v.u_id " & _
    "WHERE v.v_processed = 1 AND v.v_active = 1 AND v.v_id <> " & v_id
If Not CheckBlank(d_id) Then
    strSQL = strSQL & " OR v.d_id = " & d_id
End If
If Not CheckBlank(vm_id) Then
    strSQL = strSQL & " OR v.vm_id = " & vm_id
End If
If Not CheckBlank(vc_id) Then
    strSQL = strSQL & " OR v.vc_id = " & vc_id
End If
strSQL = strSQL & " ORDER BY v.d_id, v.vc_id, v.vm_id"

2 个答案:

答案 0 :(得分:0)

看起来你正在使用的OR条款不会带给你你想要的。假设我正确理解了片段和看不见的数据库,你需要创建一个“{1}”语句的子句,它是“ANDED”。在最初的strSQL赋值之后尝试以下内容:

OR

因此,假设您的所有检查都带有值,那么您将拥有一个如下所示的where子句:

...
dim strSubClause
strSubClause = ""

If Not CheckBlank(d_id)) Then
    strSubClause = "v.d_id = " & d_id
End If
If Not CheckBlank(vm_id) Then
    If len(strSubClause) > 0 then
        strSubClause = strSubClause & " OR v.vm_id = " & vm_id
    Else
        strSubClause = "v.vm_id = " & vm_id
    End If
End If
If Not CheckBlank(vc_id) Then
    If len(strSubClause) > 0 then
        strSubClause = strSubClause & " OR v.vc_id = " & vc_id
    Else
        strSubClause = "v.vc_id = " & vc_id
    End if
End If
If len(strSubClause) > 0 then
    strSQL = " AND (" & strSubClause & ")"
End If
strSQL = strSQL & " ORDER BY v.d_id, v.vc_id, v.vm_id"

这是否有意义和/或让你更近或一直到你想去的地方?

答案 1 :(得分:0)

我有几个想法。

您当前的查询包含一些AND和一些OR。您可能需要括号来表示您的首选操作顺序,例如:

strSQL = "SELECT TOP 4 v.v_id, vm.vm_name, v.v_year, v.v_model, v.v_search_price, d.d_name, u.u_name " & _
    "FROM tbl_Vehicles v " & _
    "LEFT JOIN tbl_VehicleMake vm ON vm.vm_id = v.vm_id " & _
    "LEFT JOIN tbl_Dealers d ON d.d_id = v.d_id " & _
    "LEFT JOIN tbl_Users u ON u.u_id = v.u_id " & _
    "WHERE v.v_processed = 1 AND v.v_active = 1 AND v.v_id <> " & v_id

If Not CheckBlank(d_id) Or Not CheckBlank(vm_id) Or Not CheckBlank(vc_id) Then
        strSQL = strSQL & "("
End If
If Not CheckBlank(d_id) Then
    strSQL = strSQL & " OR v.d_id = " & d_id
End If
If Not CheckBlank(vm_id) Then
    strSQL = strSQL & " OR v.vm_id = " & vm_id
End If
If Not CheckBlank(vc_id) Then
    strSQL = strSQL & " OR v.vc_id = " & vc_id
End If
If Not CheckBlank(d_id) Or Not CheckBlank(vm_id) Or Not CheckBlank(vc_id) Then
        strSQL = strSQL & ")"
End If
strSQL = strSQL & " ORDER BY v.d_id, v.vc_id, v.vm_id"

但根据您在问题中所写的内容,使用ORDER BY子句根据您指定的条件获取最合适的记录的查询可能会更好。

strSQL = "SELECT TOP 4 v.v_id, vm.vm_name, v.v_year, v.v_model, v.v_search_price, d.d_name, u.u_name " & _
    "FROM tbl_Vehicles v " & _
    "LEFT JOIN tbl_VehicleMake vm ON vm.vm_id = v.vm_id " & _
    "LEFT JOIN tbl_Dealers d ON d.d_id = v.d_id " & _
    "LEFT JOIN tbl_Users u ON u.u_id = v.u_id " & _
    "WHERE v.v_processed = 1 AND v.v_active = 1 AND v.v_id <> " & v_id & " ORDER BY"

If Not CheckBlank(d_id) Or Not CheckBlank(vm_id) Or Not CheckBlank(vc_id) Then
    If Not CheckBlank(d_id) Then
        strSQL = strSQL & " CASE WHEN v.d_id = " & d_id & " THEN 0 ELSE 1 END,"
    End If
    If Not CheckBlank(vm_id) Then
        strSQL = strSQL & " CASE WHEN v.vm_id = " & vm_id & " THEN 0 ELSE 1 END,"
    End If
    If Not CheckBlank(vc_id) Then
        strSQL = strSQL & " CASE WHEN v.vc_id = " & vc_id & " THEN 0 ELSE 1 END,"
    End If
    strSQL = Left(strSQL, Len(strSQL) - 1)
Else
    strSQL = strSQL & "v.d_id, v.vc_id, v.vm_id"
End If

即使没有该经销商,品牌或类别的车辆,此查询仍会给您结果,因此您将始终拥有相关车辆(假设您至少有5条记录)。