Results 1 to 5 of 5

Thread: [RESOLVED] Joining two mysql tables...

  1. #1

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

    Resolved [RESOLVED] Joining two mysql tables...

    ok guys...

    sql Code:
    1. SELECT r_category.catid, r_category.title,
    2. r_business.name, r_business.address, r_business.emailcontact
    3. FROM r_business
    4. FULL OUTER JOIN r_category ON r_business.catid = r_category.catid
    5. WHERE r_business.BusinessID=2

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OUTER JOIN r_category ON r_business.catid = r_category.catid WHERE r_business.Bu' at line 1.
    ok gues... heres my problem, i have got two tables,, r_business, and r_category

    r_category lists diff categorys of business (restaurant / hotel ETC) n links via catid in both tables.

    HOW can i get the information in one query... i only wanna select a certain business?.... i thought that was correct but guess not... any help please?
    Wayne

  2. #2

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

    Re: Joining two mysql tables...

    OK think i sorted it, should of searched harder.... i needed INNER JOIN instead of FULL OUTER JOIN ??,,,

    but... if i wanna link BusnessID to the users table which as got BusinessID as a foriegn key AS WELL... i can just add an additional row?

    sql Code:
    1. SELECT r_category.catid, r_category.title,r_business.name, r_business.address, r_business.emailcontact, r_user.username
    2. FROM r_business
    3. INNER JOIN r_category ON r_business.catid = r_category.catid
    4. INNER JOIN r_user ON r_business.BusinessID = r_user.BusinessID
    5. WHERE r_business.BusinessID=2

    is that correct?
    Wayne

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Joining two mysql tables...

    An Inner Join is standard, and probably what you want (it only returns records when there is data in both tables).

    You use some kind of Outer Join when you want to always get data from one of the tables, even if there is no matching data in the other. You would usually use a Left or Right, rather than a Full (which returns unmatching data from both tables).


    To use extra tables in the query, just add an extra Join as you did.

  4. #4

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

    Re: Joining two mysql tables...

    OK I thought i had this but obviously not... i have built my database in access to try and get it to work, but nothing... im guessing maybe theres something wrong with how i want to link the tables, but i just cant see it?.

    the image attached has the layout of wot i wish to accomplish...

    but the code im using doesnt join the tables?

    and if it does... it doesnt present any information... / records, can anyone help with this?

    php Code:
    1. $qry = "SELECT  ".$prefix."r_category.catid, ".$prefix."r_category.title, "
    2.                      .$prefix."r_state.name, "
    3.                      .$prefix."r_highway.Highway, "
    4.                      .$prefix."r_exit.exitno, "
    5.                      .$prefix."r_business.BusinessID, ".$prefix."r_business.name "
    6.                      ."FROM ".$prefix."r_business "
    7.                      ."INNER JOIN ".$prefix."r_state ON ".$prefix."r_state.stateid = ".$prefix."r_highway.State "
    8.                      ."INNER JOIN ".$prefix."r_exit ON ".$prefix."r_exit.highwayid = ".$prefix."r_highway.HiwayID "
    9.                      ."INNER JOIN ".$prefix."l_exitbusiness ON ".$prefix."l_exitbusiness.exitid = ".$prefix."r_exit.ExitID "
    10.                      ."INNER JOIN ".$prefix."r_business ON ".$prefix."l_exitbusiness.busid1 = ".$prefix."r_business.BusinessID "
    11.                      ."INNER JOIN ".$prefix."r_category ON ".$prefix."r_category.catid = ".$prefix."r_business.catid ";
    Attached Images Attached Images  
    Wayne

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Joining two mysql tables...

    Is that giving you an error? (it should be) If so, what?


    After splitting it out, I can see that you have made two major mistakes.. you have not specified all of the tables that are needed (for example, r_highway is not in there), and you have not joined to/from apt tables (eg: you effectively join r_business to itself).

    For simplicity, I have removed $prefix from the following.

    These parts are valid:
    Code:
    FROM r_business 
    INNER JOIN r_category ON r_category.catid = r_business.catid
    ..as each table is listed just once (after FROM or JOIN) and the ON clauses specify fields that are in the tables which are already listed.

    This part would be valid if you had r_highway already joined:
    Code:
    INNER JOIN r_state ON r_state.stateid = r_highway.State
    This is how it should be:
    Code:
    FROM r_business 
    INNER JOIN r_category     ON r_category.catid = r_business.catid 
    INNER JOIN l_exitbusiness ON l_exitbusiness.busid1 = r_business.BusinessID 
    INNER JOIN r_exit         ON l_exitbusiness.exitid = r_exit.ExitID 
    INNER JOIN r_highway      ON r_exit.highwayid = r_highway.HiwayID 
    INNER JOIN r_state        ON r_state.stateid = r_highway.State

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