473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 17013
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 autoincrementing 'id' field

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

Aggro <sp**********@yahoo.com> wrote in message news:<DA************@read3.inet.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****@teamnemitoff.com (Adam Nemitoff) wrote in message news:<b4**************************@posting.google. com>...
I do have an autoincrementing 'id' field

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

Aggro <sp**********@yahoo.com> wrote in message news:<DA************@read3.inet.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****@teamnemitoff.com (Adam Nemitoff)
wrote:
I do have an autoincrementing '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 autoincrementing 'id' field

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

Aggro <sp**********@yahoo.com> wrote in message news:<DA************@read3.inet.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****@teamnemitoff.com (Adam Nemitoff) wrote in message news:<b4**************************@posting.google. com>...
I do have an autoincrementing 'id' field

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

Aggro <sp**********@yahoo.com> wrote in message news:<DA************@read3.inet.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****@teamnemitoff.com (Adam Nemitoff)
wrote:
I do have an autoincrementing '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
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...
0
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...
0
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...
2
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...
2
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
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...
5
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...
2
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...
10
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.