如何使用内部联接。我不断收到错误消息

时间:2019-06-18 06:58:05

标签: inner-join

我为我的过滤器输入了一个查询,该查询需要连接2或3个表才能进行过滤。当我尝试使用内部联接时,出现内部联接不受支持的错误。我使用的数据库是ms.access。我将在评论中提供表格内容。

我尝试了此代码,但可以进行分配,但分配需要内部联接

strSQL = "SELECT m.primaryTitle, m.startYear as startYear, m.genres as genres, r.averageRating as Rating FROM basic m, Rating r where m.tconst=r.tconst AND (" & fieldselect & " LIKE '%" & TextBox1.Text & "%')"

Select Case ComboBox1.Text
            Case "year"
                fieldselect = "startYear"
            Case "genre"
                fieldselect = "genres"
            Case "rating"
                fieldselect = "averageRating"
        End Select

        conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Movies.accdb; Persist Security Info=False;")

        strSQL = "SELECT m.primaryTitle, m.startYear as startYear, m.genres as genres, r.averageRating as Rating FROM basic m INNER join Rating r on m.tconst=r.tconst (" & fieldselect & " LIKE '%" & TextBox1.Text & "%')"

        conn.Open()
        da = New OleDbDataAdapter(strSQL, conn)

        Dim ds As New DataSet("Movies")

        da.Fill(ds, "Movies")

        DataGridView1.DataSource = ds.Tables("Movies")
        conn.Close()

strSQL = "SELECT m.primaryTitle, m.startYear as startYear, m.genres as genres, r.averageRating as Rating FROM basic m, Rating r where m.tconst=r.tconst AND (" & fieldselect & " LIKE '%" & TextBox1.Text & "%')"

此代码的结果与我所期望的不符,当我从组合框选择要过滤的一项时,所有内容都显示在datagrid视图中,但是我想显示一个表的特定列或少数列。

1 个答案:

答案 0 :(得分:0)

尝试这个:

    Dim da as oledbdataadapter = new oledbdataadapter ("Select column_name from table A inner join table B on A.column_name = B.column_name WHERE column_name = '" & combobox.text & "'",oledbconnection) 
    dim dt as datatable = new datatable
    da.fill(dt)
    datagridview.datasource = dt