Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Fetching 3 rows from table
Old 09-06-2011, 02:10 AM Fetching 3 rows from table
phazorRise's Avatar
Skilled Talker

Posts: 57
Name: Sachin Gutte
Trades: 0
Hi there,
how can i fetch 3 records from table ? let me clear, suppose I've id=5 then i would like to fetch records whose id would be 4,5,6.
that means, i want to fetch previous and next records of current record.
what would be the query ?
__________________
Make everything as simple as possible, but not simpler. — Albert Einstein

Please login or register to view this content. Registration is FREE
phazorRise is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-06-2011, 05:04 AM Re: Fetching 3 rows from table
vectorialpx's Avatar
Extreme Talker

Posts: 249
Name: octavian
Location: Bucharest
Trades: 0
Let's take id = 39
Code:
SELECT idKey, otherField FROM (

( SELECT idKey, otherField FROM myTab WHERE idKey > 39 ORDER BY idKey ASC LIMIT 1 )
UNION ALL
( SELECT idKey, otherField FROM myTab WHERE idKey = 39 LIMIT 1 )
UNION ALL
( SELECT idKey, otherField FROM myTab WHERE idKey < 39 ORDER BY idKey DESC LIMIT 1 )

 ) t1
__________________
you can
Please login or register to view this content. Registration is FREE
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 09-06-2011, 06:25 AM Re: Fetching 3 rows from table
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Using what database server??

and BTW SQL is NOT PHP.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-06-2011, 06:33 AM Re: Fetching 3 rows from table
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Originally Posted by vectorialpx View Post
Let's take id = 39
Code:
SELECT idKey, otherField FROM (

( SELECT idKey, otherField FROM myTab WHERE idKey > 39 ORDER BY idKey ASC LIMIT 1 )
UNION ALL
( SELECT idKey, otherField FROM myTab WHERE idKey = 39 LIMIT 1 )
UNION ALL
( SELECT idKey, otherField FROM myTab WHERE idKey < 39 ORDER BY idKey DESC LIMIT 1 )

 ) t1
Why the UNION query?


Code:
WHERE column >= number-1 OR column <= number+1
will suffice.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-06-2011, 08:55 AM Re: Fetching 3 rows from table
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
Only if you can guarantee an arithmetic sequence diff 1 and no members missing.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is offline
Reply With Quote
View Public Profile
 
Old 09-06-2011, 09:50 AM Re: Fetching 3 rows from table
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Originally Posted by PaulW View Post
Only if you can guarantee an arithmetic sequence diff 1 and no members missing.
That's quite true, but as per the OP's question
Quote:
i would like to fetch records whose id would be 4,5,6.
It would be reasonable to expect such a scenario to exist.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-06-2011, 11:06 AM Re: Fetching 3 rows from table
vectorialpx's Avatar
Extreme Talker

Posts: 249
Name: octavian
Location: Bucharest
Trades: 0
It's about records so, when we say ID we all think at some auto_increment primary key field. You cannot write your SQL based on some presumption (that you'll never delete from that thable)
__________________
you can
Please login or register to view this content. Registration is FREE
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 09-06-2011, 12:06 PM Re: Fetching 3 rows from table
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
(that you'll never delete from that thable)
Yep BUT you can only answer questions based on the information given.

In your UNION queries you have assumed that MySQL is the database server by your use of a LIMIT clause in the query.

The OP specified records with id's of 4,5 & 6 and did not detail what server was being used


Also "deleting" information in some scenarios may not actually remove rows/records from a table.
In an accounting system for instance, invoice or ledger records HAVE to be maintained as numerically sequential and should be simply flagged as deleted/tombstoned, and not actually removed from the database to allow for an audit trail.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-06-2011, 01:14 PM Re: Fetching 3 rows from table
phazorRise's Avatar
Skilled Talker

Posts: 57
Name: Sachin Gutte
Trades: 0
Quote:
SQL is NOT PHP
yes it's not. I missed database forum.
Quote:
Why the UNION query?
i have no idea about UNION.
and 4,5,6 is for example. actual records can be like 3,6,9 and i'll be having 6 with me and have to get 3 and 9 from it.
I missed many things here. I'm using mysql. this is not accounting system i don't want to audit anything here. once something deleted it's gone. at most i'm tracking down title of the record deleted and which user/admin deleted it. planning to use trigger for this purpose. tracking data will be stored in another table. and this is so irrelevant stuff i mentioned here.
__________________
Make everything as simple as possible, but not simpler. — Albert Einstein

Please login or register to view this content. Registration is FREE
phazorRise is offline
Reply With Quote
View Public Profile
 
Old 09-06-2011, 05:13 PM Re: Fetching 3 rows from table
vectorialpx's Avatar
Extreme Talker

Posts: 249
Name: octavian
Location: Bucharest
Trades: 0
Quote:
Originally Posted by phazorRise View Post
i have no idea about UNION.
You have to have same columns to get results (and to avoid errors) and all selects are accumulated, same as a simgle one. You can select form more than one table, but maintain the column alias, like this

You have 3 columns, named c1, c2, c3.
Every subselect must return those 3 columns, named the same, even if some are simple strings.

SELECT c1, c2, c3 FROM (

( SELECT someField as c1, another as c2, other c3 FROM someTable )
UNION ALL
( SELECT SUM(field1) c1, anotherField c2, lastOne c3 FROM anotherTable )
UNION ALL
( SELECT field1 c1, anotherField c2, 'simpleString' as c3 FROM anotherTable )

) t1

You can see some examples reading mysql website


Quote:
actual records can be like 3,6,9
Of course thay can! Some system that uses keys "maintained as numerically sequential" is just wrong... it's just not designed by a programmer. Primary keys are meant to be unique, not to identify some imaginary sequence. If for some reasons those systems exists, they are just exceptions.


edit // Oh, I assumed that it's about MySql because PHP working with other DBMS is just a rare thing. So, yes Chris, I made some suppositions I didn't have no reason or intention to upset you but (sorry if I did that), I just wrote my opinion.
__________________
you can
Please login or register to view this content. Registration is FREE

Last edited by vectorialpx; 09-06-2011 at 05:26 PM..
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 09-06-2011, 05:52 PM Re: Fetching 3 rows from table
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
it's just not designed by a programmer.
Thank goodness for that!

Programmers who think they are also DBA's are the bane of a DBA's life.

And it is nothing at all to do with keys Primary or Foreign.

But if the tax office audits your system and the invoice numbers or ledger entries are NOT sequentially numbered and ALL records exist including the cancelled/deleted ones, they WILL be looking into your accounts system much more closely.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-08-2011, 09:17 AM Re: Fetching 3 rows from table
phazorRise's Avatar
Skilled Talker

Posts: 57
Name: Sachin Gutte
Trades: 0
i found following query does the thing i actually want. And it's quite simple also.

Code:
(SELECT id FROM city
   WHERE id < 5
    ORDER BY id DESC LIMIT 1)
UNION
(SELECT id FROM city
   WHERE id >= 5
   ORDER BY id LIMIT 2)
so suppose i have id=5 in my hand and records present in table as -
Code:
id otherF
2     -
5     - 
7     -
so above query returns successful result also.
__________________
Make everything as simple as possible, but not simpler. — Albert Einstein

Please login or register to view this content. Registration is FREE
phazorRise is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Fetching 3 rows from table
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.46542 seconds with 12 queries