We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 172,021 times

Contents

Related Categories

SQL Commands - Getting Records

Getting Records

If you want to retrieve some records in a table, you use the SELECT statement. To use this, all you need to know the name of the table (in this case, Users):

SELECT * FROM Users

The * tells the database program to return all the fields. Therefore, this statement returns all the records and all the fields from the Users table. If you don't want all the fields, you simply list the ones you do want:

SELECT UserID, UserName FROM Users

Don't forget, if your field or table names have spaces (which they shouldn't!), you will need to surround the field names/table name with quotes:

SELECT 'UserID', 'UserName' FROM 'Users'

Conditions

If you don't want to have all the fields returned, you need to add the WHERE clause, specifying a condition or filter. For example,

SELECT UserID, UserName FROM Users WHERE UserName = 'James'

would return all the records where the UserName field contained the value James. Note that if you are searching for a string that contains a ' you need to convert it to ''.

You can also use the < (less than), > (greater than), or <> (not) conditions too. For example,

SELECT UserID, UserName FROM Users WHERE UserID < 10

would return all the records which had a UserID of less than 10 To use more than one condition, you can use the AND/OR statements. For example,

SELECT UserID, UserName FROM Users WHERE UserID < 10 OR UserName = 'James'

would return all the records that had a UserID of less than 10, or had a UserName of James.

Sorting

You can also tell the database what order you want the records to be retrieved in by using the ORDER BY clause. To use this, you simply specify the field name to sort by, and whether you want the data sorted in Ascending (ASC) or Descending (DESC) order. For example,

SELECT * FROM Users ORDER BY UserName DESC

returns all the records from the Users table, sorted by UserName, in descending order. As the ASC option is the default option, you don't actually need to specify it if you want the records to be sorted in ascending order;

SELECT * FROM Users ORDER BY UserName

will do exactly the same as

SELECT * FROM Users ORDER BY UserName ASC

You can also instruct the database to sort using multiple fields. For example,

SELECT * FROM Users ORDER BY UserName, UserID DESC

will return all the records from the Users table, sorted by UserName in ascending order first, and then by UserID in descending order.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Check for Failed SQL Query

    Posted by zible on 24 Jan 2007

    This seems simple, yet I can't find how you do this. I need to run a query in VB on a very simple database. There is the possibility that the query would be unsuccessfull, and if so I need to do ot...

  • Sql Info

    Posted by darshan on 25 Apr 2005

    you need to fire sql statement like this
    select idno, name, email, return from [table_name] where [your_condition]

    this will return record set lets say "rs" is object of recordset
    then you will n...

  • Another database issue

    Posted by Abzmoh2007 on 11 Mar 2004

    Basically am trying to search a database by getting an input from the users selection on a list.
    the list contains names of countries and when i click that search button, how can i get the result of...

  • Filling Table With Datagrid

    Posted by NightChillz on 23 Dec 2003

    hey, i see how i can fill a datagrid with data from an sql table, but i am having trouble figuring out how to do the exact opposite, take data from a datagrid and fill an sql table with it/overriding ...

  • update data

    Posted by tomoko on 26 Nov 2003

    hi,
    pls help as i'm new in asp. i'm developing the administrative part of a website for my project. i'm having trouble with the "updating data" part. for example, i have done the 'add staff' and "de...