Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
Thread: How to select a specific cell?
02-01-2013, 04:04 AM #1
- Join Date
- Aug 2011
- Thanked 0 Times in 0 Posts
How to select a specific cell?
This might be a total newbie question but it confuses me so:
Following this table:
How do I select the 2nd cell of the first row? All the commands appear to grab complete rows or columns. I want to grab just one specific cell (and without it having a specific name)
02-01-2013, 07:00 AM #2
Ummm...the cell may not have a specific name, but the *FIELDS* (a.k.a. "columns", but that can be misleading, causing confusion with spreadsheets) MUST have names.
The 2nd cell is in the 2nd field. And the name of that field *IN ALL ROWS* is LastName.
So immediately you know to doCode:
SELECT LastName FROM Persons
The harder part is figuring out how to select the correct *RECORD* (again, a.k.a. "row", but again too easily confused with spreadsheets).
The only way, in a database, to specify a single record is to pick a field (or combination of fields) that has a *UNIQUE* value that won't match any other records and use that uniqueness in a WHERE clause.
Now, in the example in that w3c schools page, each record has a unique P_id field, so you could certainly doCode:
SELECT LastName FROM Persons WHERE P_id = 1
SELECT LastName FROM Persons WHERE FirstName = 'Ola'
SELECT LastName FROM Persons WHERE Address = 'Timoteivn 10'
because all 3 of those will pick out one and only one record, the first one listed in the table.
And here is the EVEN MORE fun part:
SELECT LastName FROM Persons WHERE LastName = 'Hansen'Code:
SELECT LastName FROM Persons WHERE City ='Sandnes'
But wait! You *could* do:Code:
SELECT LastName FROM Persons WHERE City ='Sandnes' ORDER BY P_id LIMIT 1
There are many many many more ways to select particular single records and also groups of records. And that's the whole "fun" of using SQL.Be yourself. No one else is as qualified.
02-01-2013, 07:06 AM #3
How come you didn't read the just-preceding page to the one you gave us?
Look at the example they give there:
SELECT LastName,FirstName FROM Persons
And then just combine that with the WHERE examples they give here:
In point of fact, it is usually considered *BAD PRACTICE* to use SELECT *
-- It gets all the fields for each found record, and you seldom really need all the columns
-- When working with PHP/JSP/ASP/etc. clients, it causes the client code to have to *ask* the database for all the field names and types, in order to properly interpret the results
So good queries *will* limit themselves to SELECT of only certain fields.Be yourself. No one else is as qualified.