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. Multiple selection from the same table

Multiple selection from the same table

Scheduled Pinned Locked Moved Database
helpdatabase
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.
  • C Offline
    C Offline
    CodingLover
    wrote on last edited by
    #1

    Hi all, I'm stuck with selecting multiple conditions in same table. I'll explain in this way. I've a table called tblPackages as follows. package id value code ------- ---- ------ ------ pak 1 | 1 | 0 | 58 pak 2 | 3 | 0 | 58 pak 3 | 4 | 0 | 58 pak 1 | 3 | 10 | 31 pak 2 | 2 | 100 | 31 pak 3 | 3 | 8 | 31 so I want to find all the package details that id = 1 / code = 58 and id = 3 / code = 31. So the package should be 'pak 1' I can explain the same like this. Say I execute the following query select * from tblPackages where id = 1 and code = 58, the result is package id value code ------- ---- ------ ------ pak 1 | 1 | 0 | 58 and then I execute the following query, select * from tblPackages where id = 3 and code = 31, the result is package id value code ------- ---- ------ ------ pak 1 | 3 | 10 | 31 pak 3 | 3 | 8 | 31 so the common result is, package id value code ------- ---- ------ ------ pak 1 | 1 | 0 | 58 pak 1 | 3 | 10 | 31 That's what I want to get. From that later I want to get either code of 58 data or code of 31 data. Can anyone of you comment on me. I've try to get data separately and merge manually, but it's not easy as i though. thanks

    I appreciate your help all the time... CodingLover :)

    S W L 3 Replies Last reply
    0
    • C CodingLover

      Hi all, I'm stuck with selecting multiple conditions in same table. I'll explain in this way. I've a table called tblPackages as follows. package id value code ------- ---- ------ ------ pak 1 | 1 | 0 | 58 pak 2 | 3 | 0 | 58 pak 3 | 4 | 0 | 58 pak 1 | 3 | 10 | 31 pak 2 | 2 | 100 | 31 pak 3 | 3 | 8 | 31 so I want to find all the package details that id = 1 / code = 58 and id = 3 / code = 31. So the package should be 'pak 1' I can explain the same like this. Say I execute the following query select * from tblPackages where id = 1 and code = 58, the result is package id value code ------- ---- ------ ------ pak 1 | 1 | 0 | 58 and then I execute the following query, select * from tblPackages where id = 3 and code = 31, the result is package id value code ------- ---- ------ ------ pak 1 | 3 | 10 | 31 pak 3 | 3 | 8 | 31 so the common result is, package id value code ------- ---- ------ ------ pak 1 | 1 | 0 | 58 pak 1 | 3 | 10 | 31 That's what I want to get. From that later I want to get either code of 58 data or code of 31 data. Can anyone of you comment on me. I've try to get data separately and merge manually, but it's not easy as i though. thanks

      I appreciate your help all the time... CodingLover :)

      S Offline
      S Offline
      slam Iqbal
      wrote on last edited by
      #2

      It is not clear that what is your problem. As i understood your problem is to view both results of those query in a single output. To that you have to use 'UNION'.

      select * from tblPackages where id = 1 and code = 58
      UNION
      select * from tblPackages where id = 3 and code = 31

      It'll produce this output: package id value code ------- ---- ------ ------ pak 1 | 1 | 0 | 58 pak 1 | 3 | 10 | 31 You have to use 'DISTINCT' to eliminate duplicate entries. You can also try using JOIN but I can't understand what is you problem.

      1 Reply Last reply
      0
      • C CodingLover

        Hi all, I'm stuck with selecting multiple conditions in same table. I'll explain in this way. I've a table called tblPackages as follows. package id value code ------- ---- ------ ------ pak 1 | 1 | 0 | 58 pak 2 | 3 | 0 | 58 pak 3 | 4 | 0 | 58 pak 1 | 3 | 10 | 31 pak 2 | 2 | 100 | 31 pak 3 | 3 | 8 | 31 so I want to find all the package details that id = 1 / code = 58 and id = 3 / code = 31. So the package should be 'pak 1' I can explain the same like this. Say I execute the following query select * from tblPackages where id = 1 and code = 58, the result is package id value code ------- ---- ------ ------ pak 1 | 1 | 0 | 58 and then I execute the following query, select * from tblPackages where id = 3 and code = 31, the result is package id value code ------- ---- ------ ------ pak 1 | 3 | 10 | 31 pak 3 | 3 | 8 | 31 so the common result is, package id value code ------- ---- ------ ------ pak 1 | 1 | 0 | 58 pak 1 | 3 | 10 | 31 That's what I want to get. From that later I want to get either code of 58 data or code of 31 data. Can anyone of you comment on me. I've try to get data separately and merge manually, but it's not easy as i though. thanks

        I appreciate your help all the time... CodingLover :)

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #3

        Hi, You didn't quite specify what relates those to rows so I assume it's the package. In that case could have something like:

        SELECT *
        FROM tblPackages main
        WHERE Id IN (1,3)
        AND Code IN (58, 31)
        AND Package = (SELECT DISTINCT Package
        FROM (SELECT Package
        FROM tblPackages
        WHERE id = 1
        AND code = 58) a,
        (SELECT Package
        FROM tblPackages
        WHERE id = 3
        AND code = 31) b
        WHERE a.Package = b.Package)

        Don't mind about typos etc, it's not tested at all. In the above if you can have several matching package use IN instead of equality for the package comparison. Also the statement can be simplified so this is just one version.

        The need to optimize rises from a bad design.My articles[^]

        1 Reply Last reply
        0
        • C CodingLover

          Hi all, I'm stuck with selecting multiple conditions in same table. I'll explain in this way. I've a table called tblPackages as follows. package id value code ------- ---- ------ ------ pak 1 | 1 | 0 | 58 pak 2 | 3 | 0 | 58 pak 3 | 4 | 0 | 58 pak 1 | 3 | 10 | 31 pak 2 | 2 | 100 | 31 pak 3 | 3 | 8 | 31 so I want to find all the package details that id = 1 / code = 58 and id = 3 / code = 31. So the package should be 'pak 1' I can explain the same like this. Say I execute the following query select * from tblPackages where id = 1 and code = 58, the result is package id value code ------- ---- ------ ------ pak 1 | 1 | 0 | 58 and then I execute the following query, select * from tblPackages where id = 3 and code = 31, the result is package id value code ------- ---- ------ ------ pak 1 | 3 | 10 | 31 pak 3 | 3 | 8 | 31 so the common result is, package id value code ------- ---- ------ ------ pak 1 | 1 | 0 | 58 pak 1 | 3 | 10 | 31 That's what I want to get. From that later I want to get either code of 58 data or code of 31 data. Can anyone of you comment on me. I've try to get data separately and merge manually, but it's not easy as i though. thanks

          I appreciate your help all the time... CodingLover :)

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Huh? I think this[^] holds the answer, and

          ... WHERE (id = 1 AND code = 58) OR (id = 3 AND code = 31)

          should do it.

          CodingLover wrote:

          all the package details that id = 1 / code = 58 and id = 3 / code = 31

          I guess your problem is linguistic: the "and" in the above sentence isn't really an "and" (a row couldn't have ID=1 AND ID=3 at the same time) it is more of an "and also", which actually indicates an "or" situation. :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          W 1 Reply Last reply
          0
          • L Luc Pattyn

            Huh? I think this[^] holds the answer, and

            ... WHERE (id = 1 AND code = 58) OR (id = 3 AND code = 31)

            should do it.

            CodingLover wrote:

            all the package details that id = 1 / code = 58 and id = 3 / code = 31

            I guess your problem is linguistic: the "and" in the above sentence isn't really an "and" (a row couldn't have ID=1 AND ID=3 at the same time) it is more of an "and also", which actually indicates an "or" situation. :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #5

            Wouldn't that lead to a situation where the row

            pak 3 | 3 | 8 | 31

            is included? I got the impression that it should be eliminated from the result.

            The need to optimize rises from a bad design.My articles[^]

            L 1 Reply Last reply
            0
            • W Wendelius

              Wouldn't that lead to a situation where the row

              pak 3 | 3 | 8 | 31

              is included? I got the impression that it should be eliminated from the result.

              The need to optimize rises from a bad design.My articles[^]

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              yes it would. however I found "so I want to find all the package details that id = 1 / code = 58 and id = 3 / code = 31. So the package should be 'pak 1'" a bit confusing, it would result in two rows IMO. The enquirer will have to make up his mind and tell us what he really wants. :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              W 1 Reply Last reply
              0
              • L Luc Pattyn

                yes it would. however I found "so I want to find all the package details that id = 1 / code = 58 and id = 3 / code = 31. So the package should be 'pak 1'" a bit confusing, it would result in two rows IMO. The enquirer will have to make up his mind and tell us what he really wants. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                W Offline
                W Offline
                Wendelius
                wrote on last edited by
                #7

                Luc Pattyn wrote:

                The enquirer will have to make up his mind and tell us what he really wants

                That's true :)

                The need to optimize rises from a bad design.My articles[^]

                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