SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Do I use JOIN or a separate query

    I have three tables:

    Code:
    form_1
    =================================================
    app_id	| f_name	| l_name	| admin_id
    -------------------------------------------------
    0	| Paul		| Simon		| 25
    1	| Art		| Garfunkel	| 25
    
    
    form_2
    =================================================
    app_id	| f_name	| l_name	| admin_id
    -------------------------------------------------
    0	| Jack		| White		| 25
    1	| Meg		| White		| 25
    
    
    admins
    =================================================
    admin_id| f_name	| l_name	| admin_id
    -------------------------------------------------
    0	| Frank		| Gallagher	| 25
    The tables have many more fields, and I'd like to show a handful of the same fields from both form_1 and form_2.

    When displaying the data, I also need to show the admin's f_name and l_name, by way of their ID.

    I had this working fine when there was only form_1 and the admin table:
    Code:
    SELECT
    	form_1.app_id,
    	form_1.f_name, 
    	form_1.l_name,
    	admins.f_name,
    	admins.l_name
    FROM form_1
    	LEFT OUTER
    		JOIN admins
    			ON admins.admin_id = form_1.admin_id
    But how to add form_2 to this is throwing me off. I'm wondering if it would be better practice to get the admin table out of the query and into a separate array to check against using PHP? Or if I should somehow join the three tables in the same query?

  2. #2
    SitePoint Zealot
    Join Date
    Feb 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops! I used admin_id twice in the admins table. It's not actually like that, I just mistyped. There is only 1 admin_id in that table and it is unique.

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Code:
    SELECT app_id 
         , f_name 
         , l_name 
         , admin_id
      FROM form_1
    UNION ALL
    SELECT app_id 
         , f_name 
         , l_name 
         , admin_id
      FROM form_2
    UNION ALL
    SELECT admin_id 
         , f_name 
         , l_name 
         , admin_id
      FROM admins
    ORDER
        BY admin_id
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •