SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2004
    Location
    UK
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Joining tables which aren't directly related

    Hi,

    I'm struggling to get my head round a SQL query and wonder if you might be able to help (I'm using MySQL version 4.1.20-1).

    I have three tables: accounts, items and invoices.

    Code:
    CREATE TABLE accounts (
        id INT UNSIGNED NOT NULL AUTO_INCREMENT,
        ...
        PRIMARY KEY (id)
        ) ENGINE = InnoDB;
    
    CREATE TABLE items (
        id INT UNSIGNED NOT NULL AUTO_INCREMENT,
        accounts_id INT UNSIGNED,
        ...
        PRIMARY KEY (id),
        FOREIGN KEY (accounts_id) REFERENCES accounts (id) ON DELETE SET NULL
        ) ENGINE = InnoDB;
    
    CREATE TABLE invoices (
        id INT UNSIGNED NOT NULL AUTO_INCREMENT,
        items_id INT UNSIGNED,
        ...
        PRIMARY KEY (id),
        FOREIGN KEY (items_id) REFERENCES items (id) ON DELETE CASCADE
        ) ENGINE = InnoDB;
    Each account can have multiple items and each item can have multiple invoices. Hope you're with me so far?

    So here's the question. If I have an account ID, how do I get a list of invoice IDs for that account when accounts and invoices aren't directly related?

    Thanks for taking the time.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,251
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by jez View Post
    If I have an account ID, how do I get a list of invoice IDs for that account when accounts and invoices aren't directly related?
    Code:
    select invoices.id
      from accounts
    inner
      join items
        on items.accounts_id = accounts.id
    inner
      join invoices
        on invoices.items_id = items.id
     where accounts.id = 937
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    select ...
      from items i
      join invoices inv
        on inv.items_id = i.id
     where i.accounts_id = 29292929

  4. #4
    SitePoint Member
    Join Date
    Jun 2004
    Location
    UK
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    Code:
    select invoices.id
      from accounts
    inner
      join items
        on items.accounts_id = accounts.id
    inner
      join invoices
        on invoices.items_id = items.id
     where accounts.id = 937
    Perfect - thank you!


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
  •