473,748 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SELECT statement using WHERE that involves value from "next" row?

Is is possible to construct a SELECT statement that contains a WHERE
clause that uses the value from a column in the "next" row?

ie. given a table with a single field named "myField" with the
following values I want a SELECT statement that selects the rows
"WHERE myField='1' and [NEXT ROW]myField = '2' ":

5
6
1
2
1
3
Jul 19 '05 #1
8 17050
Adam Nemitoff wrote:
Is is possible to construct a SELECT statement that contains a WHERE
clause that uses the value from a column in the "next" row?

ie. given a table with a single field named "myField" with the
following values I want a SELECT statement that selects the rows
"WHERE myField='1' and [NEXT ROW]myField = '2' ":


You _do_ know that rows _are not_ in any specific order in the database.
i.e. It is not giaranteed that they are in the same order you inserted
them? And query like that looks very suspicious, which might cause
problems in the near future _if_ you somehow would get it to work (I
don't know any way, except by doing two queries. )

However I might suggest you another way to solve this problem. What if
you would add another column to your table, which would hold that mystic
value from "next" row?

You might also want to add id-field to your table with auto_increment
value, so that you know in what order your rows were inserted, and the
"next row" would have some meaning.

Jul 19 '05 #2
Adam Nemitoff wrote:
Is is possible to construct a SELECT statement that contains a WHERE
clause that uses the value from a column in the "next" row?

ie. given a table with a single field named "myField" with the
following values I want a SELECT statement that selects the rows
"WHERE myField='1' and [NEXT ROW]myField = '2' ":


You _do_ know that rows _are not_ in any specific order in the database.
i.e. It is not giaranteed that they are in the same order you inserted
them? And query like that looks very suspicious, which might cause
problems in the near future _if_ you somehow would get it to work (I
don't know any way, except by doing two queries. )

However I might suggest you another way to solve this problem. What if
you would add another column to your table, which would hold that mystic
value from "next" row?

You might also want to add id-field to your table with auto_increment
value, so that you know in what order your rows were inserted, and the
"next row" would have some meaning.

Jul 19 '05 #3
I do have an autoincrementin g 'id' field

Thereform my query is semantically this:
SELECT ROW(id) WHERE item1='foo' AND ROW(id+1).item1 ='bar'

Aggro <sp**********@y ahoo.com> wrote in message news:<DA******* *****@read3.ine t.fi>...
Adam Nemitoff wrote:
Is is possible to construct a SELECT statement that contains a WHERE
clause that uses the value from a column in the "next" row?

ie. given a table with a single field named "myField" with the
following values I want a SELECT statement that selects the rows
"WHERE myField='1' and [NEXT ROW]myField = '2' ":


You _do_ know that rows _are not_ in any specific order in the database.
i.e. It is not giaranteed that they are in the same order you inserted
them? And query like that looks very suspicious, which might cause
problems in the near future _if_ you somehow would get it to work (I
don't know any way, except by doing two queries. )

However I might suggest you another way to solve this problem. What if
you would add another column to your table, which would hold that mystic
value from "next" row?

You might also want to add id-field to your table with auto_increment
value, so that you know in what order your rows were inserted, and the
"next row" would have some meaning.

Jul 19 '05 #4
go****@teamnemi toff.com (Adam Nemitoff) wrote in message news:<b4******* *************** ****@posting.go ogle.com>...
I do have an autoincrementin g 'id' field

Thereform my query is semantically this:
SELECT ROW(id) WHERE item1='foo' AND ROW(id+1).item1 ='bar'

Aggro <sp**********@y ahoo.com> wrote in message news:<DA******* *****@read3.ine t.fi>...
Adam Nemitoff wrote:
Is is possible to construct a SELECT statement that contains a WHERE
clause that uses the value from a column in the "next" row?

ie. given a table with a single field named "myField" with the
following values I want a SELECT statement that selects the rows
"WHERE myField='1' and [NEXT ROW]myField = '2' ":


You _do_ know that rows _are not_ in any specific order in the database.
i.e. It is not giaranteed that they are in the same order you inserted
them? And query like that looks very suspicious, which might cause
problems in the near future _if_ you somehow would get it to work (I
don't know any way, except by doing two queries. )

However I might suggest you another way to solve this problem. What if
you would add another column to your table, which would hold that mystic
value from "next" row?

You might also want to add id-field to your table with auto_increment
value, so that you know in what order your rows were inserted, and the
"next row" would have some meaning.


There is a solution, but you need to think carefully about why you are
having this difficulty - working with databases requires understanding
of a few concepts, eg, that rows are not in a particular order. What
happenes for example, if user A writes a row to the database and gets
id 5, but before he has written his following row, user B writes and
gets id 6 (this may happen in milliseconds). Even transactions will
not ensure that a given user gets sequential id numbers, because you
are bending a database concept. You must link rows by the metadata
that associates them, eg by having a 'ParentId', which is populated in
the second row with the id of the first row.

Anyway, the way I believe you may be able to implement your problem,
is preferably using the ParentId idea before, then joining a table to
itself, as follows:

SELECT fieldlist
FROM tablename t1, tablename t2
WHERE (t1.Id + 1)=t2.Id
AND t1.myField='1'
AND t2.myField='2'

Bear in mind however that this query will link ALL consecutive rows,
not just the pairs as they were created (eg, id 1 will be linked with
id2, and id 2 will be linked with id 3, etc).
Jul 19 '05 #5
On 17 Dec 2003 16:42:12 -0800, go****@teamnemi toff.com (Adam Nemitoff)
wrote:
I do have an autoincrementin g 'id' field

Thereform my query is semantically this:
SELECT ROW(id) WHERE item1='foo' AND ROW(id+1).item1 ='bar'


If you do a "self join", you might be able to get what you are trying
for.

select * from mytable as table1, mytable as table2 where
table2.id = table1.id+1

Chuck Gadd
http://www.csd.net/~cgadd/aqua
Jul 19 '05 #6
I do have an autoincrementin g 'id' field

Thereform my query is semantically this:
SELECT ROW(id) WHERE item1='foo' AND ROW(id+1).item1 ='bar'

Aggro <sp**********@y ahoo.com> wrote in message news:<DA******* *****@read3.ine t.fi>...
Adam Nemitoff wrote:
Is is possible to construct a SELECT statement that contains a WHERE
clause that uses the value from a column in the "next" row?

ie. given a table with a single field named "myField" with the
following values I want a SELECT statement that selects the rows
"WHERE myField='1' and [NEXT ROW]myField = '2' ":


You _do_ know that rows _are not_ in any specific order in the database.
i.e. It is not giaranteed that they are in the same order you inserted
them? And query like that looks very suspicious, which might cause
problems in the near future _if_ you somehow would get it to work (I
don't know any way, except by doing two queries. )

However I might suggest you another way to solve this problem. What if
you would add another column to your table, which would hold that mystic
value from "next" row?

You might also want to add id-field to your table with auto_increment
value, so that you know in what order your rows were inserted, and the
"next row" would have some meaning.

Jul 19 '05 #7
go****@teamnemi toff.com (Adam Nemitoff) wrote in message news:<b4******* *************** ****@posting.go ogle.com>...
I do have an autoincrementin g 'id' field

Thereform my query is semantically this:
SELECT ROW(id) WHERE item1='foo' AND ROW(id+1).item1 ='bar'

Aggro <sp**********@y ahoo.com> wrote in message news:<DA******* *****@read3.ine t.fi>...
Adam Nemitoff wrote:
Is is possible to construct a SELECT statement that contains a WHERE
clause that uses the value from a column in the "next" row?

ie. given a table with a single field named "myField" with the
following values I want a SELECT statement that selects the rows
"WHERE myField='1' and [NEXT ROW]myField = '2' ":


You _do_ know that rows _are not_ in any specific order in the database.
i.e. It is not giaranteed that they are in the same order you inserted
them? And query like that looks very suspicious, which might cause
problems in the near future _if_ you somehow would get it to work (I
don't know any way, except by doing two queries. )

However I might suggest you another way to solve this problem. What if
you would add another column to your table, which would hold that mystic
value from "next" row?

You might also want to add id-field to your table with auto_increment
value, so that you know in what order your rows were inserted, and the
"next row" would have some meaning.


There is a solution, but you need to think carefully about why you are
having this difficulty - working with databases requires understanding
of a few concepts, eg, that rows are not in a particular order. What
happenes for example, if user A writes a row to the database and gets
id 5, but before he has written his following row, user B writes and
gets id 6 (this may happen in milliseconds). Even transactions will
not ensure that a given user gets sequential id numbers, because you
are bending a database concept. You must link rows by the metadata
that associates them, eg by having a 'ParentId', which is populated in
the second row with the id of the first row.

Anyway, the way I believe you may be able to implement your problem,
is preferably using the ParentId idea before, then joining a table to
itself, as follows:

SELECT fieldlist
FROM tablename t1, tablename t2
WHERE (t1.Id + 1)=t2.Id
AND t1.myField='1'
AND t2.myField='2'

Bear in mind however that this query will link ALL consecutive rows,
not just the pairs as they were created (eg, id 1 will be linked with
id2, and id 2 will be linked with id 3, etc).
Jul 19 '05 #8
On 17 Dec 2003 16:42:12 -0800, go****@teamnemi toff.com (Adam Nemitoff)
wrote:
I do have an autoincrementin g 'id' field

Thereform my query is semantically this:
SELECT ROW(id) WHERE item1='foo' AND ROW(id+1).item1 ='bar'


If you do a "self join", you might be able to get what you are trying
for.

select * from mytable as table1, mytable as table2 where
table2.id = table1.id+1

Chuck Gadd
http://www.csd.net/~cgadd/aqua
Jul 19 '05 #9

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

Similar topics

8
3722
by: joe | last post by:
Can anyone help me out by either telling me how to get to the result i need or by pointing me to some documentation about the following question: A certain query to a database give me eg 100 results: $query = "select id, name, addr1, addr2, city from mytable where id=" ..$id; $connection = mysql_connect(etc etc) ....
0
2476
by: Eric Wichterich | last post by:
Hello Pythonistas, does anyone know an equivalent python statement for perl's "next"-statement? (Perl's "next" skips a loop cycle and continues with the next loop cycle - it does not abort the whole loop). Thank you! Eric
0
685
by: Adam Nemitoff | last post by:
Is is possible to construct a SELECT statement that contains a WHERE clause that uses the value from a column in the "next" row? ie. given a table with a single field named "myField" with the following values I want a SELECT statement that selects the rows "WHERE myField='1' and myField = '2' ": 5 6 1
2
1870
by: John Smith | last post by:
I have more than 4,000 pieces of email in my Yahoo account. I would like to write a program to download them all. In order to write such a program, I need to know a few things. 1. After I fill out userid, password, and click on "login", what exactly is sent to Yahoo? 2. When I click on "next" to fetch the next mail, what exactly is sent to Yahoo?
2
2456
by: ntsNews | last post by:
Hi, Using the built in command "Find Next" is there a way to have the Match Drop Down Menu to default to "Start of Field"... or can it be the only option? GCM
7
17196
by: bikesandcars | last post by:
Hello guys, I stumbled upon this forum recently and am hoping someone here can help me with this problem. This is probably very stupid, but I can't get past this easy problem / glitch. All I need to do is test if a table exists, and then delete it if it does (so I can create a new one). Of course, you can't delete a table if it doesn't exist, and this returns an error. Every source I've looked at says an easy way to solve this...
5
21788
by: Nick Gilbert | last post by:
Hi, I'm using the asp:Wizard control and on some of the steps, I would only like the user to be able to progess to the next step by clicking an image button. Therefore I would like to be able to hide the Next button on these steps. Is there any way to do this? I know you can convert the navigation to a template, but this is a
2
3831
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
how do I code generic functions to return the next item in an enumeration a) sorted by name, b) sorted by value c) sorted by declaration in a round-robin style ? for example the enum is Enum Colors Red = 5 Green = 7 Blue = 16 Yellow = 2
10
4797
by: craig.keightley | last post by:
I am trying to get the next row within a loop for a script i am developing... I need to display a final table row within the table that i have displayed on the page, but i only want to show it if value of the current field is not the same value of the next row. eg:
0
8983
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9236
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6792
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6072
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.