• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Cant quite get my head around SQL Inner Join.

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    #11
    Code:
     
    select 
             prices.price, 
             products.product_name, 
             discounts.discount_name
      from prices
     inner join products
        on prices.product_id = products.product_id
     inner join basket
        on basket.product_id = products.product_id
     inner join discounts
        on discount.discount_id = baskets.discount_id
    But that will only work if every price has an associated product
    and every product is in the basket table
    and every basket has an associated discount.

    you'll probably want to use LEFT OUTER join rather than INNER JOIN, depends on the database design.
    Coffee's for closers

    Comment


      #12
      Excellent, that works. The only snag is that if there is no discount (the id is zero) it doesn't return anything, but I can work around that with a dummy entry in the discounts table.

      Big thanks, and I promise to answer any C++ or x86 assembler questions you may have in future.
      Will work inside IR35. Or for food.

      Comment


        #13
        Originally posted by VectraMan View Post
        Excellent, that works. The only snag is that if there is no discount (the id is zero) it doesn't return anything, but I can work around that with a dummy entry in the discounts table.
        STOP!! I covered this eventuality in my earlier post.

        You'll want to use this

        Code:
        select 
                 prices.price, 
                 products.product_name, 
                 discounts.discount_name
          from prices
         inner join products
            on prices.product_id = products.product_id
         inner join basket
            on basket.product_id = products.product_id
         left outer join discounts
            on discount.discount_id = baskets.discount_id
        Coffee's for closers

        Comment


          #14
          And that works too. I think I actually understand that now. Thanks.
          Will work inside IR35. Or for food.

          Comment


            #15
            Originally posted by Spacecadet View Post

            for a simple inner join like yours, you could have them either way round
            Many thanks! That was the real question I wanted answering.

            TBH I only use basic SQL queries so although your further examples may make things easier, it tends to go over my head.

            Cheers though.
            I couldn't give two fornicators! Yes, really!

            Comment


              #16
              Originally posted by Durbs View Post
              And dont do what the tossrag who wrote the procs i'm currently looking at and alias everything with incrementing letters of the alphabet so in a big procedure i'm constantly looking back to remember what h.code is (its the Country table, course it is, silly me should have realised that from the 'h'!)
              I worked with a guy who always gave the table a really meaningful alias. Which most of the time was longer than the name of the table in the first place.
              Best Forum Advisor 2014
              Work in the public sector? You can read my FAQ here
              Click here to get 15% off your first year's IPSE membership

              Comment

              Working...
              X