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. How to query exclusion? SQL Help needed

How to query exclusion? SQL Help needed

Scheduled Pinned Locked Moved Database
databasehelptutorialquestion
8 Posts 2 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
    Kyudos
    wrote on last edited by
    #1

    I'm only a very occasional SQL user, so I'm not sure if this is even possible - but if it is, can someone help me with the SQL? The situation: In Table 1, I have a list of components, each with an ID and a type. In Table 2, I have a mapping between Outlet components and Nozzle components (two columns with component IDs in both, lets call them Out_IDs and Nozz_IDs, an Outlet may be linked to many nozzles). I'm trying to find all the Nozzle components that are not linked to any outlets. So in pseudo-SQL: SELECT * FROM Components WHERE (Component.[Type] = Nozzle) AND (Component.[ID] Is Not One Of Mapping.[Nozz_IDs]) It's the AND... bit I can't figure out, probably some sort of JOIN I can't fathom. Thanks all, Dan

    K 1 Reply Last reply
    0
    • K Kyudos

      I'm only a very occasional SQL user, so I'm not sure if this is even possible - but if it is, can someone help me with the SQL? The situation: In Table 1, I have a list of components, each with an ID and a type. In Table 2, I have a mapping between Outlet components and Nozzle components (two columns with component IDs in both, lets call them Out_IDs and Nozz_IDs, an Outlet may be linked to many nozzles). I'm trying to find all the Nozzle components that are not linked to any outlets. So in pseudo-SQL: SELECT * FROM Components WHERE (Component.[Type] = Nozzle) AND (Component.[ID] Is Not One Of Mapping.[Nozz_IDs]) It's the AND... bit I can't figure out, probably some sort of JOIN I can't fathom. Thanks all, Dan

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

      Bah! :-D Of course I figure it out as soon as I ask for help!

      SELECT * FROM Components LEFT JOIN Nozzles ON Components.[Component Key] = Nozzles.[Nozzle Key]
      WHERE (((Components.[Database Number])=15) AND ((Nozzles.[Nozzle Key]) Is Null));

      M 1 Reply Last reply
      0
      • K Kyudos

        Bah! :-D Of course I figure it out as soon as I ask for help!

        SELECT * FROM Components LEFT JOIN Nozzles ON Components.[Component Key] = Nozzles.[Nozzle Key]
        WHERE (((Components.[Database Number])=15) AND ((Nozzles.[Nozzle Key]) Is Null));

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Ain't is the way. Just some advice. Notice that you need [] around column names with spaces and special characters, eliminate these so [Component Key] becomes ComponentKey, life will be much more pleasant. Also NEVER use key words in a column name (Key and Database) I know these will not be key words when concatenated but it still applies.

        Never underestimate the power of human stupidity RAH

        K 3 Replies Last reply
        0
        • M Mycroft Holmes

          Ain't is the way. Just some advice. Notice that you need [] around column names with spaces and special characters, eliminate these so [Component Key] becomes ComponentKey, life will be much more pleasant. Also NEVER use key words in a column name (Key and Database) I know these will not be key words when concatenated but it still applies.

          Never underestimate the power of human stupidity RAH

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

          Thanks for the advice, I'll bear it in mind for the future. Unfortunately, I've inherited this MDB structure based on an old Fortran record structure so changing the names is not so easy!

          1 Reply Last reply
          0
          • M Mycroft Holmes

            Ain't is the way. Just some advice. Notice that you need [] around column names with spaces and special characters, eliminate these so [Component Key] becomes ComponentKey, life will be much more pleasant. Also NEVER use key words in a column name (Key and Database) I know these will not be key words when concatenated but it still applies.

            Never underestimate the power of human stupidity RAH

            K Offline
            K Offline
            Kyudos
            wrote on last edited by
            #5

            Can you see anything wrong with this?

            CDaoDatabase* pDB = static_cast<CMainFrame *>(AfxGetMainWnd())->GetDatabase();
            CMyRecords Records(pDB);

            CString sSQL = "SELECT * FROM Components LEFT JOIN Nozzles ON
            Components.[Component Key] = Nozzles.[Nozzle Key] WHERE
            (((Components.[Database Number])=15) AND ((Nozzles.[Nozzle Key]) Is Null))";

            try
            {
            Records.Open(dbOpenDynaset, sSQL, 0);

            if (Records.IsBOF() || Records.IsEOF())
            {
                return;
            }
            
            Records.MoveFirst();
            while (!Records.IsEOF())
            {
                if (Records.CanUpdate())
                {
                    Records.Delete();
                }
                Records.MoveNext();
            }
            
            Records.Close();
            

            }
            catch(CDaoException *e)
            {
            e->ReportError();
            e->Delete();
            if (Records.IsOpen())
            {
            Records.Close();
            }
            }

            I can see it gets the right records, it doesn't give anY errors or exceptions, but it also doesn't delete the records! Must be missing something...

            M 1 Reply Last reply
            0
            • M Mycroft Holmes

              Ain't is the way. Just some advice. Notice that you need [] around column names with spaces and special characters, eliminate these so [Component Key] becomes ComponentKey, life will be much more pleasant. Also NEVER use key words in a column name (Key and Database) I know these will not be key words when concatenated but it still applies.

              Never underestimate the power of human stupidity RAH

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

              The MessageBoard fairies strike again! If I change the SQL to the much simpler:

              "SELECT * FROM Components WHERE (Components.[Database Number])=15 AND Components.[Component Key] NOT IN (SELECT [Nozzle Key] FROM Nozzles)"

              it all works.

              1 Reply Last reply
              0
              • K Kyudos

                Can you see anything wrong with this?

                CDaoDatabase* pDB = static_cast<CMainFrame *>(AfxGetMainWnd())->GetDatabase();
                CMyRecords Records(pDB);

                CString sSQL = "SELECT * FROM Components LEFT JOIN Nozzles ON
                Components.[Component Key] = Nozzles.[Nozzle Key] WHERE
                (((Components.[Database Number])=15) AND ((Nozzles.[Nozzle Key]) Is Null))";

                try
                {
                Records.Open(dbOpenDynaset, sSQL, 0);

                if (Records.IsBOF() || Records.IsEOF())
                {
                    return;
                }
                
                Records.MoveFirst();
                while (!Records.IsEOF())
                {
                    if (Records.CanUpdate())
                    {
                        Records.Delete();
                    }
                    Records.MoveNext();
                }
                
                Records.Close();
                

                }
                catch(CDaoException *e)
                {
                e->ReportError();
                e->Delete();
                if (Records.IsOpen())
                {
                Records.Close();
                }
                }

                I can see it gets the right records, it doesn't give anY errors or exceptions, but it also doesn't delete the records! Must be missing something...

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                Without going into the code the first point I would make is that deleting a record while inside a collection changes the collection. So you move to the first record and delete it, you collection hanged and where am I supposed to move from. Try starting from the last record in the collection, delete that and move forward 1 record, or go to the last record in the collection. Basically change the way your loop works.

                Never underestimate the power of human stupidity RAH

                K 1 Reply Last reply
                0
                • M Mycroft Holmes

                  Without going into the code the first point I would make is that deleting a record while inside a collection changes the collection. So you move to the first record and delete it, you collection hanged and where am I supposed to move from. Try starting from the last record in the collection, delete that and move forward 1 record, or go to the last record in the collection. Basically change the way your loop works.

                  Never underestimate the power of human stupidity RAH

                  K Offline
                  K Offline
                  Kyudos
                  wrote on last edited by
                  #8

                  DaoRecordsets don't work like that - presumably for the very reason you describe! 'Delete' simply marks a record as deleted and doesn't alter the record order. this also allows you to rollback changes if necessary. It's all working now though, so thanks for your input!

                  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