Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Sorry in advanced for this brain dead question

Sorry in advanced for this brain dead question

Scheduled Pinned Locked Moved Database
questiondatabase
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike654321
    wrote on last edited by
    #1

    This is probably easy...I am just brain dead...I am using Ms Sql 2008, and given the following two tables: Table1 --------------- Field1 Field2 3 55 3 55 4 55 5 55 3 66 3 66 3 77 4 77 3 88 Table2 --------------- Field1 Field2 3 9 4 10 5 11 Table1 and Table2 are linked together by the Field1's...I get a parameter passed in like "9" for Table2's Field2...how do I return all of the records from Table1 of just the 66's and the one record of 88?

    N T 2 Replies Last reply
    0
    • M Mike654321

      This is probably easy...I am just brain dead...I am using Ms Sql 2008, and given the following two tables: Table1 --------------- Field1 Field2 3 55 3 55 4 55 5 55 3 66 3 66 3 77 4 77 3 88 Table2 --------------- Field1 Field2 3 9 4 10 5 11 Table1 and Table2 are linked together by the Field1's...I get a parameter passed in like "9" for Table2's Field2...how do I return all of the records from Table1 of just the 66's and the one record of 88?

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      SELECT Field2 FROM TABLE1 WHERE Field1 = (SELECT Field1 FROM Table2 WHERE Field2 = 9) AND Field2 IN(66,88)


      I know the language. I've read a book. - _Madmatt

      M 1 Reply Last reply
      0
      • N Not Active

        SELECT Field2 FROM TABLE1 WHERE Field1 = (SELECT Field1 FROM Table2 WHERE Field2 = 9) AND Field2 IN(66,88)


        I know the language. I've read a book. - _Madmatt

        M Offline
        M Offline
        Mike654321
        wrote on last edited by
        #3

        Thank you, but I don't want to hard code the 66 and 88 as those values I constantly changing. I just wanted those records as they don't have other linking fields of the 4 and 5 for example.

        N 1 Reply Last reply
        0
        • M Mike654321

          This is probably easy...I am just brain dead...I am using Ms Sql 2008, and given the following two tables: Table1 --------------- Field1 Field2 3 55 3 55 4 55 5 55 3 66 3 66 3 77 4 77 3 88 Table2 --------------- Field1 Field2 3 9 4 10 5 11 Table1 and Table2 are linked together by the Field1's...I get a parameter passed in like "9" for Table2's Field2...how do I return all of the records from Table1 of just the 66's and the one record of 88?

          T Offline
          T Offline
          Tim Carmichael
          wrote on last edited by
          #4

          Start with: select t1.* from t1 inner join t2 on t1.field1 = t2.field1 where t2.field2 = @passedvalue But, why is the (3,55) pair excluded? Tim

          M 1 Reply Last reply
          0
          • T Tim Carmichael

            Start with: select t1.* from t1 inner join t2 on t1.field1 = t2.field1 where t2.field2 = @passedvalue But, why is the (3,55) pair excluded? Tim

            M Offline
            M Offline
            Mike654321
            wrote on last edited by
            #5

            55 is excluded because it has a link of 4 and 5...and 77 is excluded because it has a link of 4 in it. Table1 --------------- Field1 -Field2 3 -55 3 -55 4 -55 5 -55 3 -66 3 -66 3 -77 4 -77 3 -88 Table2 --------------- Field1 -Field2 3 -9 4 -10 5 -11

            J 1 Reply Last reply
            0
            • M Mike654321

              Thank you, but I don't want to hard code the 66 and 88 as those values I constantly changing. I just wanted those records as they don't have other linking fields of the 4 and 5 for example.

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              Of course you don't want them hard coded, this was just an example. :rolleyes:


              I know the language. I've read a book. - _Madmatt

              1 Reply Last reply
              0
              • M Mike654321

                55 is excluded because it has a link of 4 and 5...and 77 is excluded because it has a link of 4 in it. Table1 --------------- Field1 -Field2 3 -55 3 -55 4 -55 5 -55 3 -66 3 -66 3 -77 4 -77 3 -88 Table2 --------------- Field1 -Field2 3 -9 4 -10 5 -11

                J Offline
                J Offline
                Jorgen Andersson
                wrote on last edited by
                #7

                Mike654321 wrote:

                55 is excluded because it has a link of 4 and 5...and 77 is excluded because it has a link of 4 in it.

                That was useful info. Is it something like this you need?

                SELECT Table1.field2
                FROM Table1 join Table2
                ON Table1.field1 = Table2.field1
                WHERE Table2.field2 = 9
                AND Table1.field2 NOT IN
                (
                SELECT DISTINCT Table1.field2
                FROM Table1 join Table2
                ON Table1.field1 = Table2.field1
                WHERE Table2.field2 <> 9
                )
                ;

                This query might be very slow if the tables are big. which can be cured with composite indexes on both tables.

                My postings are a natural product. The slight variations in spelling and grammar enhance their individual character and beauty and are in no way to be considered flaws or defects.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups