SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JOIN without a common column

    Say I have:

    Table1
    User_ID
    User_Data

    Table2
    User_ID1 (Table1.UserID value)
    User_ID2 (Table1.UserID value)

    And I need to get
    Table1.User_Data WHERE Table1.User_ID=? (required)
    Table2.User_ID1 WHERE = Table1.User_ID (optional)
    Table2.User_ID2 WHERE = Table1.User_ID (optional)

    I will always have a value for Table1.User_Data

    I also need to return either User_ID1 OR User_ID2 ONLY IF either one matches Table1.User_ID. There doesn't have to be a value returned from this table.

    What would this query look like? Would I use a JOIN? It doesn't seem to make use of one, because of the totally optional 2nd table returning any values.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,274
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    yes, you would use a join, but there are so many variations, i dunno what to suggest

    Table2 seems to link two users together, so here's a query that shows pairs of users linked through Table2
    Code:
    select A.User_Data, B.User_Data
      from Table2
         , Table1 as A
         , Table1 as B
     where Table2.User_ID1 = A.User_ID
       and Table2.User_ID2 = B.User_ID
    give me some more ideas of what you want, because there are many other queries that are possible here..

    rudy
    http://rudy.ca/

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will throw an error if Table1.User_Data = 0 or the User_ID supplied exists in either column of Table2

    I could do something like this psuedo-query:
    SELECT Table1.User_Data WHERE Table1.User_ID=?, Count(WHERE Table2.User_ID1=?) AS UserCount1, Count(Table2.User_ID2=?) AS UserCount2

    ? = the User ID from the page. Then I can check if Table1.User_Data > 0, and if either COUNT > 0 in my code.
    Last edited by Metallisoft; Sep 13, 2002 at 23:54.


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
  •