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. Select Statement in SQL Server

Select Statement in SQL Server

Scheduled Pinned Locked Moved Database
questiondatabasesql-serversysadmin
6 Posts 3 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.
  • K Offline
    K Offline
    KeithF
    wrote on last edited by
    #1

    Hi Folks, I have a quick question in relation to how i select data from a table, as seen in the table below i want to get rows where Person1 & Person2 are both on the same row, so in the table below i would just want Rows 1 & 2. Table: ID | Person1 | Person2 | . . . 1 | ABC | DEF | . . . 2 | DEF | ABC | . . . 3 | ABC | GHI | . . . . . . I have tried the following:

    Select * From MyTable Where Person1 = 'ABC' OR Person1 = 'DEF AND Person2 = 'ABC' OR Person2 = 'DEF'

    This does not have the desired effect however. Where am i going wrong here?

    K 1 Reply Last reply
    0
    • K KeithF

      Hi Folks, I have a quick question in relation to how i select data from a table, as seen in the table below i want to get rows where Person1 & Person2 are both on the same row, so in the table below i would just want Rows 1 & 2. Table: ID | Person1 | Person2 | . . . 1 | ABC | DEF | . . . 2 | DEF | ABC | . . . 3 | ABC | GHI | . . . . . . I have tried the following:

      Select * From MyTable Where Person1 = 'ABC' OR Person1 = 'DEF AND Person2 = 'ABC' OR Person2 = 'DEF'

      This does not have the desired effect however. Where am i going wrong here?

      K Offline
      K Offline
      KeithF
      wrote on last edited by
      #2

      Nevermind figured it out, was missing brackets in the SQL statement

      KeithF wrote:

      Select * From MyTable Where (Person1 = 'ABC' OR Person1 = 'DEF) AND (Person2 = 'ABC' OR Person2 = 'DEF')

      G R 2 Replies Last reply
      0
      • K KeithF

        Nevermind figured it out, was missing brackets in the SQL statement

        KeithF wrote:

        Select * From MyTable Where (Person1 = 'ABC' OR Person1 = 'DEF) AND (Person2 = 'ABC' OR Person2 = 'DEF')

        G Offline
        G Offline
        GuyThiebaut
        wrote on last edited by
        #3

        This is another way of getting the same result:

        Select * From MyTable Where Person1 in('ABC','DEF') AND Person2 in('ABC','DEF')

        “That which can be asserted without evidence, can be dismissed without evidence.”

        ― Christopher Hitchens

        K 1 Reply Last reply
        0
        • G GuyThiebaut

          This is another way of getting the same result:

          Select * From MyTable Where Person1 in('ABC','DEF') AND Person2 in('ABC','DEF')

          “That which can be asserted without evidence, can be dismissed without evidence.”

          ― Christopher Hitchens

          K Offline
          K Offline
          KeithF
          wrote on last edited by
          #4

          Thanks GuyThiebaut, that is cleaner code.

          1 Reply Last reply
          0
          • K KeithF

            Nevermind figured it out, was missing brackets in the SQL statement

            KeithF wrote:

            Select * From MyTable Where (Person1 = 'ABC' OR Person1 = 'DEF) AND (Person2 = 'ABC' OR Person2 = 'DEF')

            R Offline
            R Offline
            Richard Deeming
            wrote on last edited by
            #5

            KeithF wrote:

            Where (Person1 = 'ABC' OR Person1 = 'DEF) AND (Person2 = 'ABC' OR Person2 = 'DEF')

            Your query will also return rows where Person1 and Person2 are the same value. It's not obvious from your description whether that would be a problem, or even if it's possible in your database. If you only want rows which have both people on the row, try:

            WHERE 'ABC' IN (Person1, Person2) And 'DEF' In (Person1, Person2)

            or the slightly longer version:

            WHERE (Person1 = 'ABC' And Person2 = 'DEF') Or (Person1 = 'DEF' And Person2 = 'ABC')


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            K 1 Reply Last reply
            0
            • R Richard Deeming

              KeithF wrote:

              Where (Person1 = 'ABC' OR Person1 = 'DEF) AND (Person2 = 'ABC' OR Person2 = 'DEF')

              Your query will also return rows where Person1 and Person2 are the same value. It's not obvious from your description whether that would be a problem, or even if it's possible in your database. If you only want rows which have both people on the row, try:

              WHERE 'ABC' IN (Person1, Person2) And 'DEF' In (Person1, Person2)

              or the slightly longer version:

              WHERE (Person1 = 'ABC' And Person2 = 'DEF') Or (Person1 = 'DEF' And Person2 = 'ABC')


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              K Offline
              K Offline
              KeithF
              wrote on last edited by
              #6

              Thanks Richard, It would not be possible for ABC to exist in Person1 and Person2 in the same row, but its no harm using the syntax you describe to circumvent it in case it were to happen.

              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