Results 1 to 11 of 11

Thread: to JOIN or INNER JOIN, that is the question...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271

    to JOIN or INNER JOIN, that is the question...

    Hi,

    I have two tables (well that are required for this function anyway)

    - tbl_Items
    - tbl_Bids

    and this is my code:

    VB Code:
    1. Sub imgcmd_Search_Click(sender As Object, e As ImageClickEventArgs)
    2.    Dim  MyConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & _
    3.         server.mappath("cgi-bin/db/orctions.mdb"))
    4.    Dim CommandText As String = "SELECT * FROM tbl_Items WHERE SearchData like '%" &  txt_Search.text & "%'"
    5.  
    6.    Dim myCommand As New OleDBCommand(CommandText, myConnection)
    7.         myConnection.Open()
    8.         rep_Search.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    9.         rep_Search.DataBind()
    10. End Sub

    what I need to do is get the Fields 'Bidder' and 'BidValue' from 'tbl_Bids' as long as it matches the critera in the Where Clause which is looking at the 'tbl_Itmes'

    I hope that is clear enough ... ?

    Any help would be great thanks
    §tudz

    Studzworld.com - Portfolio

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131
    Can you list the fields in both tables and state the relationship between them if it isn't that obvious.

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464

    Re: to JOIN or INNER JOIN, that is the question...

    Originally posted by §tudz
    Hi,

    I have two tables (well that are required for this function anyway)

    - tbl_Items
    - tbl_Bids

    and this is my code:

    VB Code:
    1. Sub imgcmd_Search_Click(sender As Object, e As ImageClickEventArgs)
    2.    Dim  MyConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & _
    3.         server.mappath("cgi-bin/db/orctions.mdb"))
    4.    Dim CommandText As String = "SELECT * FROM tbl_Items WHERE SearchData like '%" &  txt_Search.text & "%'"
    5.  
    6.    Dim myCommand As New OleDBCommand(CommandText, myConnection)
    7.         myConnection.Open()
    8.         rep_Search.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    9.         rep_Search.DataBind()
    10. End Sub

    what I need to do is get the Fields 'Bidder' and 'BidValue' from 'tbl_Bids' as long as it matches the critera in the Where Clause which is looking at the 'tbl_Itmes'

    I hope that is clear enough ... ?

    Any help would be great thanks
    You should try something like this. You need to fill in the common table column between them (identified with the SOMECOLUMN word)
    "SELECT b.Bidder, b.BidValue FROM tbl_Items a
    JOIN tbl_Bids b,
    ON a.SOMECOLUMN = b.SOMECOLUMN
    WHERE a.SearchData like '%" & txt_Search.text & "%'"

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Also, that would work in SQL Server, but might not in Access.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271
    what should the real values be from b and a ? are they the tble names?
    §tudz

    Studzworld.com - Portfolio

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271
    this is the sql I used:


    "SELECT b.*, a.* FROM tbl_Items a JOIN tbl_Bids b, ON a.ItemCode = b.ItemCode WHERE a.SearchData like '%" & txt_Search.text & "%'"


    But this just errors?
    §tudz

    Studzworld.com - Portfolio

  7. #7
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131
    Try:

    "SELECT b.*, a.* FROM tbl_Items AS a INNER JOIN tbl_Bids AS b, ON a.ItemCode = b.ItemCode WHERE a.SearchData like '%" & txt_Search.text & "%'"

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271
    I'm afraid that didn't work either,

    This is the Error:

    Server Error in '/' Application.
    --------------------------------------------------------------------------------

    Syntax error in FROM clause.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Data.OleDb.OleDbException: Syntax error in FROM clause.
    §tudz

    Studzworld.com - Portfolio

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271
    I've got it to work now with this:


    "SELECT tbl_Items.ItemCode, tbl_Items.Owner, tbl_Items.ItemName, tbl_Items.SearchData, tbl_Items.IsSold, tbl_Items.ShortDescription, tbl_Items.BidClosingDate, tbl_Items.SearchData, tbl_Bids.Bidder, tbl_Bids.BidValue, tbl_Bids.Time FROM tbl_Items INNER JOIN tbl_Bids ON tbl_Items.ItemCode = tbl_Bids.ItemCode WHERE tbl_Items.SearchData like '%" & txt_Search.text & "%'"
    §tudz

    Studzworld.com - Portfolio

  10. #10
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Sorry, in SQL you can alias tables, that is where the a and b came from. Access won't let you do that, you have to specify the tablename.field explicitly.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271
    thanks, anyway, wouldn't have had a clue to even start with the statement, so you helped loads.

    Thanks again
    §tudz

    Studzworld.com - Portfolio

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width