473,574 Members | 2,260 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get the First record

Hi all,

Is there a MySQL function to get the first record through a query? I would like to open a page of
client records with the first one shown. The other records can be accessed from a hyperlinked list.

Thanks for any advice,

Rick
Jul 17 '05 #1
17 4455
Rick wrote:
Is there a MySQL function to get the first record through a
query? I would like to open a page of client records with the
first one shown. The other records can be accessed from a
hyperlinked list.


Define "first record" :)

There is no such thing as far as the database is concerned. If you want
to retrieve a single record from the database say so in the query:

select *one* record from the database:
select <field, list> from <tables> where <conditions>
LIMIT 1

select *the first record* from the database
select <field, list> from <tables> where <conditions>
ORDER BY <sort, fields> limit 1
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
If you only want to return the first row from an unknown number then add
"LIMIT 1 OFFSET 0" to the SELECT statement. Check out the syntax at
http://www.mysql.com/documentation/m...ax.html#SELECT

--
Tony Marston

http://www.tonymarston.net
"Rick" <RB@newsgroup.n et> wrote in message
news:11Y0c.9775 8$Xp.432840@att bi_s54...
Hi all,

Is there a MySQL function to get the first record through a query? I would like to open a page of client records with the first one shown. The other records can be accessed from a hyperlinked list.
Thanks for any advice,

Rick

Jul 17 '05 #3
I noticed that Message-ID: <11Y0c.97758$Xp .432840@attbi_s 54> from Rick
contained the following:
Is there a MySQL function to get the first record through a query? I would like to open a page of
client records with the first one shown. The other records can be accessed from a hyperlinked list.


Data is not stored in any particular order in a database. Therefore the
concept of 'first record' is meaningless.

Presumably you mean the oldest record. Either use a field containing a
timestamp or a primary key that auto increments and order the results by
one of those.

For example

$result = mysql_query("SE LECT * FROM table ORDER BY id ");
//default is ascending

//to fetch the first record
$myrow = mysql_fetch_arr ay($result)
print $myrow["fieldname1 "];

//loop through rest of records
while($myrow = mysql_fetch_arr ay($result))
{
//do stuff with records
}


--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #4
Geoff Berrow wrote:
I noticed that Message-ID: <11Y0c.97758$Xp .432840@attbi_s 54> from Rick
contained the following:
Is there a MySQL function to get the first record through a query? I
would like to open a page of client records with the first one
shown. The other records can be accessed from a hyperlinked list.


Data is not stored in any particular order in a database. Therefore
the
concept of 'first record' is meaningless.


Actually, this is not always true... While data may be stored unordered, the
data may also be stored in an ordered manner, usually in the order defined
by an index on the relation.
Jul 17 '05 #5
in which case you still are talking about random records, but indexed by an
indexfield....
First record, still is something undefined, whereas record with lowest
indexnumber might have a "first record" feel to it.

Michel

"Agelmar" <if**********@c omcast.net> wrote in message
news:c2******** *****@ID-30799.news.uni-berlin.de...
Geoff Berrow wrote:
I noticed that Message-ID: <11Y0c.97758$Xp .432840@attbi_s 54> from Rick
contained the following:
Is there a MySQL function to get the first record through a query? I
would like to open a page of client records with the first one
shown. The other records can be accessed from a hyperlinked list.
Data is not stored in any particular order in a database. Therefore
the
concept of 'first record' is meaningless.


Actually, this is not always true... While data may be stored unordered,

the data may also be stored in an ordered manner, usually in the order defined
by an index on the relation.

Jul 17 '05 #6
Agelmar wrote:
Actually, this is not always true... While data may be stored unordered, the
data may also be stored in an ordered manner, usually in the order defined
by an index on the relation.


The key word there is *may*.

And the DB server *may* return the records in the order they are stored.
Suppose you have a table with a ID (auto_increment primary key) column.

You have no guarentee that
"select id from table where id between 7890 and 7891"

will return the record with ID 7890 before the other record; the result
*could* very well be
+------+
| ID |
+------+
| 7891 |
| 7890 |
+------+
and *may* even change between calls to the same query!

So, if need the records returned in a specific order, specify that
order in the select command.
<?php
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 0;
$sql = 'select id, name, email from people limit ' . ($page*20) . ', 20';
// ...
?>

does *NOT* guarantee you will not see the same people on page 7 as you
saw on page 6 (or does it?).
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #7
Pedro Graca wrote:
Rick wrote:
Is there a MySQL function to get the first record through a
query? I would like to open a page of client records with the
first one shown. The other records can be accessed from a
hyperlinked list.

Define "first record" :)

There is no such thing as far as the database is concerned. If you want
to retrieve a single record from the database say so in the query:

select *one* record from the database:
select <field, list> from <tables> where <conditions>
LIMIT 1

select *the first record* from the database
select <field, list> from <tables> where <conditions>
ORDER BY <sort, fields> limit 1


What I want to do is fill a page that displays record details with some data even if the user did
not select a customer id. I just want to grab one record...either first by id or last name (asc),
but one record only.

The 'Limit' clause as suggested by both Pedro and Tony appears to be what I want.

Thanks,

Rick
Jul 17 '05 #8
I noticed that Message-ID: <JB41c.166553$u V3.715478@attbi _s51> from Rick
contained the following:
What I want to do is fill a page that displays record details with some data even if the user did
not select a customer id. I just want to grab one record...either first by id or last name (asc),
but one record only.
Yes but you also said,

"The other records can be accessed from a hyperlinked list."

I assumed you wanted to show the full details of one record and possibly
a summary of all others such that you could simply click them and get
the details of that record. How are you going to create the list if you
have only accessed one row using LIMIT ?
The 'Limit' clause as suggested by both Pedro and Tony appears to be what I want.


Not if I have understood your scenario correctly.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #9
Pedro Graca wrote:
Agelmar wrote:
Actually, this is not always true... While data may be stored
unordered, the data may also be stored in an ordered manner, usually
in the order defined by an index on the relation.


The key word there is *may*.

<snip>
No arguments. I was just responding to the specific sentence "Data is not
stored in any particular order in a database."
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8785
by: Richard C Buchanan | last post by:
I have a table as follows: - user_id (key) - user_email_address (text) - user_request_date (text) - user_sent_date (time/date) An automated macro will run every 5 minutes and needs to run a SQL UPDATE that:
4
12988
by: KJ | last post by:
We use a third party software package for acctg that limits us on field size - as a result, we at times have to enter an item into the app multiple times in order to get all the information. I then created an Access DB that is linked to the information and have combined all data onto one record so that we can export to excel. The next step...
3
2221
by: Jason A. Thompson | last post by:
Dear Access Gurus, I have a database which I hoped to use to administer questionnaires, or rather that someone who knows nothing about Access could use to administer them. Each q'aire item is on a different form and I dropped in command buttons on each to move forward and backward through the "pages" of the q'aire. Problem is, the first...
6
5093
by: neptune | last post by:
I must be missing something obvious. I have 3 fields and sample data. 7890 26592 7/15/2003 7890 13645 10/6/1999 7890 58741 6/21/1992 I want to select only the most recent record. It is sorted descending by in the query and subquery. In the query Total, I select Group By , First and...
5
5240
by: tdmailbox | last post by:
I have a form with a child form. In the child form there is a list of names that can grow quite large. On the parent form I want to display the first name from the child form. I set up a test box that is populated with the code =subfrm_media_review_sec_party.Form!first_name & " " & subfrm_media_review_sec_party.Form!last_name It...
10
3721
by: Lyn | last post by:
I have a form set to Single Form mode with which I can cycle through the records in a table via Next and Previous buttons. To avoid users pressing the Previous button on the first record and the Next button on the last record, I would like to disable one or both buttons when the first and/or last record is displayed. I am not sure how to do...
1
1420
by: Karthiga1984 | last post by:
Hi Table Structure company name Phone No Products Example Company Name Phone No Products
1
6480
by: Hubert Wisniewski | last post by:
Hi, I have a problem with first record in my table. I put the data to the table and it's ok, but if I add second row to the table the first record is updated (and has the same value as the second row). I use the same code for three tables in my application and it works without any problem with this one exception. If I have the first item...
2
6387
by: ofuuzo1 | last post by:
Hei, I have the following xml file and I have tried to write xslt to extract only the values of the first "record" node. It does not work. I need some help. I used ---- to represent indent. Xml: <?xml version="1.0" encoding="UTF-8"?> <OAI-PMH xmlns="http://www.openarchives.....OAI/2.0/OAI-PMH.xsd">...
0
7826
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7741
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8126
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6491
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5328
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3763
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3781
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1090
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.