Results 1 to 6 of 6

Thread: SQL Filtering

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    4

    Question

    I am currently filtering a table from a database using the SELECT FROM sql command. Does anyone know how I can then filter the results from the first query to produce another set of results?

    Can this be done with the SELECT INTO command and if so how do you use this command?

    Thanks
    Tom

  2. #2
    Hyperactive Member
    Join Date
    May 1999
    Location
    Reynosa, Mexico
    Posts
    274
    May be you can write a subquery:

    SELECT * FROM table2 WHERE fieldX = (SELECT FieldY FROM table1)

    But, fieldX and fieldY must be of the same type.

    If you explain more details maybe I can do more explicit.

    Good Look!
    Ulises Vázquez
    [size=1.7]Oracle DBA Certified Professioanl
    Visual Basic 6 Developer
    Crystal Reports Designer
    [/size]

  3. #3
    Fanatic Member Gaffer's Avatar
    Join Date
    Nov 2000
    Location
    London
    Posts
    828
    Depends on what database you’re using really.

    If Access, you create your first SELECT FROM statement and save as a query (e.g. qryFirst). Then build your second SELECT FROM based on the query you have just created (e.g.
    SELECT * FROM qryFirst WHERE qryFirst.field1 LIKE ‘k*’)

    If SQL Server, (or anything based on Trans-Act SQL), you avoid having to create a new table by nesting SELECT statements.

    For example;

    SELECT field1, field2 FROM (SELECT * from tblTable)

    This is simple logic to the Access example.

    Hoep this clarifies,


  4. #4
    Fanatic Member Gaffer's Avatar
    Join Date
    Nov 2000
    Location
    London
    Posts
    828

    Wink

    Tom,

    Please be aware the two examples posted by myself and Tonatiuh

    are different. My example takes the original table, filters it, and filters again.
    The other example from Tonatiuh queries a table using criteria based on the results of a query on another separate table.

    Both are valid, but give different results…

    Gaffer

  5. #5
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    Don't forget that you can use IN

    SELECT Field2 FROM Table2 WHERE Field2 IN (SELECT Field1 FROM Table1);

    That construct can be quite useful.

    Cheers,

    Paul.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can also filter it.
    open the recordset as SELECT Field2 FROM Table2 WHERE Field2 and then rs.filter="Field1='Somethingelse'".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width