Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-01-2013, 03:04 AM   PM User | #1
Jeffy
New to the CF scene

 
Join Date: Aug 2011
Posts: 5
Thanks: 3
Thanked 0 Times in 0 Posts
Jeffy is an unknown quantity at this point
How to select a specific cell?

This might be a total newbie question but it confuses me so:

Following this table:
http://www.w3schools.com/sql/sql_distinct.asp
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)
Jeffy is offline   Reply With Quote
Old 02-01-2013, 06:00 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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 do
Code:
SELECT LastName FROM Persons
(assuming you are using that "Persons" table on that page as an example database table).

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 do
Code:
SELECT LastName FROM Persons WHERE P_id = 1
But here is the fun part: You could also do
Code:
SELECT LastName FROM Persons WHERE FirstName = 'Ola'
or
SELECT LastName FROM Persons WHERE Address = 'Timoteivn 10'
[/code]
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:
Code:
SELECT LastName FROM Persons WHERE LastName = 'Hansen'
But you could *NOT* do
Code:
SELECT LastName FROM Persons WHERE City ='Sandnes'
because that query will find *TWO* records, not just one.

But wait! You *could* do:
Code:
SELECT LastName FROM Persons WHERE City ='Sandnes'
            ORDER BY P_id LIMIT 1
The ORDER_BY ensures that you will find the records in the given order and the LIMIT 1 says "I only want the first record, in the given order, that matches."

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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-01-2013, 06:06 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
How come you didn't read the just-preceding page to the one you gave us?

http://www.w3schools.com/sql/sql_select.asp

Look at the example they give there:
SELECT LastName,FirstName FROM Persons

And then just combine that with the WHERE examples they give here:
http://www.w3schools.com/sql/sql_where.asp

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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:18 AM.


Advertisement
Log in to turn off these ads.