SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    joining multiple tables.

    Hi

    I have two queries, very similar, and I would like to create one query out of these two. I tried to check in my little sql book how to join multiple tables but it is not explained
    Code:
    		$sql = @mysql_query(
     		"SELECT userstexts.textID
     		, userstexts.textTitle
    	 	, userstexts.textText
     		, users.userID
     		, users.username 
     		FROM userstexts
     		INNER JOIN users 
     		ON userstexts.textID = $textID
     		AND users.userID = $memberID
     		LIMIT 1"); 
    		echo mysql_error();
    Code:
    		
    				$textinfo = @mysql_query(
     		"SELECT userstexts.textID
    		, userstexts.textTitle
    		, userstexts.category
    		, userstexts.postedDate
    		, userstexts.userID
    		, categories.catName 
    		FROM userstexts
    		INNER JOIN categories
    		ON userstexts.category = categories.catID
    		WHERE userID = $memberID 
    		ORDER 
    		BY userstexts.postedDate DESC"); 
    		echo mysql_error();
    thanks a lot for your time and patience

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,022
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Code:
    select userstexts.textID
         , userstexts.textTitle
         , userstexts.textText
         , userstexts.postedDate
         , userstexts.category
         , categories.catName 
         , users.userID
         , users.username 
      from userstexts
    inner 
      join users 
        on userstexts.userID = users.userID
    inner 
      join categories
        on userstexts.category = categories.catID
     where users.userID = $memberID     
       and userstexts.textID = $textID
    order 
        by userstexts.postedDate desc
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot rudy

    so if I want to join multiple tables, all I have to do is use many INNER JOIN statements. In your query, users is joined to userstexts, and then categories is joined to userstexts right?


  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,022
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    that is correct
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok cool now I got it btw, do you have a good book that you would recommend about MySql rudy? I was thinking about getting a MySql cookbook. I reall need to improve myself in this field.

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,022
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    you may find several good resources (not books) on my SQL Links page
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot rudy

    you rock!


Bookmarks

Posting Permissions

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