Tuesday, July 28, 2009

ASP and Access: How to pull only specific rows from DB in recordset?

Can someone tell me how to pull only certain rows from an Access DB using ASP?





I have one table in my database that allows multiple records with the same data in one of the fields (stockNum). I want to pull only one record for each unique record. So, if there are five records in this table with a stock number of 58025, I only want to pull one record with that stock number.





Is this possible?

ASP and Access: How to pull only specific rows from DB in recordset?
You need to know a bit about SQL queries to do this.





In Access:





1. Create a query


2. Switch to SQL view


3. Begin your query this way:





SELECT DISTINCT column1





where column1 is the name of your stock number column.





4. Finish your query by listing off all the other columns you want:





column2, column3, column4 FROM tablename





where column1, column2 and column3, etc are the names of additional columns you want to display; and tablename is the name of the table they are in.





Your finished query should look something like this:





SELECT DISTINCT column1, column2, column3, column4 FROM tablename





5. Save the query with a name you can remember.





6. In ASP, to specify you want to use the query, just treat it like a table.
Reply:'--- Use this query do select only unique stockNum :) happy coding


sqlQuery = "Select Distinct stockNum From TableName"








'--- this query will return unique column1 AND column2 AND column 3 AND column 4 altogether


SELECT DISTINCT column1, column2, column3, column4 FROM tablename





i.e.


column 1 column 2


1---------------------1


1---------------------2


1---------------------3


2---------------------1


2---------------------2





but if you use the first query... it will return ONLY the unique stockNum


No comments:

Post a Comment