Wednesday, May 28, 2008

Querying a database with SELECT

The thing done most often to databases is querying. This means asking the database for a row of information, called a 'record'. This contains one joined set of values, for example "John Smith" "19" "Level 5" would correspond to values such as "Name", "Age", "Rank" in the database.

SELECT Name FROM Persons

That select query would then return a 'result set' of all names within the database, such as "John Smith" "Jake Prower" and "Mike Huggabee".

SELECT Name,Age FROM Persons

If you need to get more information, such as Name and Age, then do something like the above, which will return a couple sets of data (arrays, you would probably call them), one set with the Names, and the other with the Ages (but they will be linked in those sets).

SELECT DISTINCT Name FROM Persons

If you use the DISTINCT keyword, sql will only return a set of unique names. You won't get two "John Smith"s.

No comments: