Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  Advanced Search
  HOME    TRACKER    MYSQL


SuperbHosting.net have generously sponsored dedicated servers to ensure a reliable and scalable dedicated hosting solution for BigResource.com.





Previous, Current, And Next In One Query?


This situation has come up a couple times and I've wanted to know if there's a better way to do it.

Given something unique (an ID number, a specific date/time, etc) I want to find the thing that comes before it and the one after it when sorted by something else. Oh, and the "something unique" isn't sequential - random intervals between each.

Code: ....




View Complete Forum Thread with Replies

Related Forum Messages:
Get Previous And Next ID Of Current Record
I want do create a "Previous" and a "Next" link, all I have is the ID of the current record. Is there a good way to get the previous and next id of a record with a single SQL Query?

example:

ID's: 4,10,7,9,13

I open my page and show all details from the record with the ID 7, now I want to create the links and I need the previous and the next ID, 10 and 9.

I could make a query that returns only the ID's and loop through the recordset till i have my ID's but that's such an ugly solution, not to mention the performance issues.

View Replies !
Comparing Difference Between Current Record And Previous, Non-sequential/variable Data
I have a locally-developed asset tracking system in which inventory operators upload batches of time stamped CSV data into a “move_log” via a handheld barcode scanner after they take an inventory of what assets are in a particular location.

I would like to be able to identify each time an item actually moved from one location to the next. Sounds simple, however:

1. An item may be inventoried multiple times without actually moving (think large items that don’t move very often, if ever). This means an entry in the “move_log” table for each inventory sweep, even if no change in location.
2. An item may move from one location to another, then back to the original location, perhaps several times. Think items such as laptops kept in a central depot and checked out for use. This means that a simple “SELECT DISTINCT” will not show the actual number of moves, just the number of different places an asset has been.

Unless I am missing something, the best way I can think to describe a “move” is to look at the entries for each asset, and then compare with the previous entry for that asset. Any difference between the current entry and the previous would count as one “move.” Of course, there is no set number of entries for a particular asset. Some assets may have been inventoried once or twice, others dozens of times.

I would like to do this entirely within MySQL if possible.

The table in question: ...

View Replies !
Query Based On Results Of A Previous Query
So far I have managed to construct one query which gives me all individuals that have one of three titles.

based on this I now want to find all the individuals that are affiliated to those listed in the first query ....

View Replies !
Update Query Fails In Previous MySQL Version
The following query runs fine on my development server (v4.0.17):

"update user inner join events on user.username = events.username
set user.LastMessageDate = '$current_time'
where events.text_sent=0 and events.to_mobile >0 and events.GMT_event_send_time<='$send_time'"

but fails on my production server running v3.23.56.

Anyone any ideas why it doesn't run and what I need to do to fix it?

View Replies !
Sql Query For Count Records Of Current Week
I have a registration form that saves the registration date on a datetime column. I would like make a query to know how many records are in the table that where for the current week. For example, I'm on week 25 and I want to know how many records I have for this week without the need of specifying the from and to dates. Is there a way to do this?

View Replies !
Query That Finds Last Row In Relation To Current Row (read For More)
It's been almost a year since I last did SQL and I can't for the life of me figure out how to do this again.

Pretty much I have a table like so:

id | ip | time

I want to get a result set that contains each unique ip, how many times it appears in the table, and the last time it appears.

View Replies !
How To Use Previous Select Query Results In New Select Query?
I have tested everything and it works OK in its current form. However, what I need help with is the part of the query highlighted in red.

As it currently stands, the PHP while statement loops through the rows and echos out the html. The issue I have is that it echos out the same speaker name and subject type for each iteration. Now I know this is happening because I have set subject.event_id = 1, so what I need is to use the event_id of the current iteration as the clause and that is what I need help with....

View Replies !
List All Clients Not Assigned To Current User (was "Query Help Please")
I have two tables like this:

*clients
-- client_id
-- client_name

*manage
-- user_id (multiple entries possible)
-- client_id

To assign clients to users there is a entry in the manage table of the
user_id and client id.

I want to list all the client_ids and client_names that are not assigned to the current user_id say 1.

View Replies !
Subtract Previous Value.
I'm trying to get one column of numbers to subtract from another column and to keep a running total. Column A is a dollar amount from an invoice. Column B starts out with an opening amount ($10,000) and I want to have Column A to subtract from that.
Code:


COLUMN A--------COLUMN B
$0.00 $10,000
$1000 $9,000
$500 $8,500
$2000 $6,500

View Replies !
ID Of Previous Insert
Im thinking i need a transaction here. I want to insert a record into a table, which has an autoincrement field as the primary id. Then use this id to insert into another table. In the time between the first insert and then getting the id, couldnt there possibly be another user that causes an insert and hence the id the first user gets back isnt correct ?transactions to the rescue ?

View Replies !
Previous Record
I need to get a previous record from database - i need to retrieve records which differs from the previous one about 5s example:

select eventDate,previousEventDate,eventDate-previousEventDate as delta from events having delta<5 order by eventDate;

eventDate is normal column from table

previousEventDate is an "alias" for the previous date ... how can I write it in SQL?
Please help. I can use SELECT as subquery, but it would be probably really slow, the table will have 100000s maybe milions of records soon.

View Replies !
Previous Row Comparison
I have a table in this format:

Column A | Column B
1 | 2
1 | 3
1 | 4
2 | 5
.....
.....

What I want to do is something like this:

If in Column A there is a change in the number i.e. from the above table there were many 1s which transitioned into a 2. I want to record this change. I want to extract these two rows into another table. How can I make this possible?

View Replies !
Using Data From Previous Row
I only want to select the current row if a field in the previous row is not null.

Is it possible to write such a query?

View Replies !
Previous To Last With A Certain WHERE Condition
How do i fetch the previous row record with the same WHERE when the last record has only a lately date.

for example

field CConsorcio CRubro ImporteA FechaA

1 105 1000 2006-05-10
2 110 30 2006-05-12
5 105 300 2006-05-13
4 104 600 2006-05-14
2 103 800 2006-05-15

the WHERE CConsorcio=2
the last record: 2,103,800,2006-05-15
and i want to select the row: 2,110,30,2006-05-12

View Replies !
Get Next And Previous Records
Our website has a portfolio database which uses a column entitled 'list_order' to set the order of how we want the projects to display in a list. Each project has a link to click into it to see the full details of the project.

Inside this details page we would like to add a View Previous Project and View Next Project link which would allow the user to navigate through our projects. The problem is that we are using an order set by 'list_order' and not an auto incremented column.

How would I setup the Query to grab the previous and next rows no matter which project the user clicked on? For example, our database has these records:

id - list_order
102 - 100
89 - 200
7 - 250
30 - 300
97 - 400
20 - 500
62 - 600
88 - 650
103 - 700
85 - 800
112 - 900
95 - 1000

So, if someone clicked into id 97, we'd need a query that would grab id numbers 30 and 20 for the previous and next id to create the links.

View Replies !
Select Previous And Next Id After Id=# ?

For a gallery I'm trying to query for a picture by its id and also get some images before and after that id (according to current sort) are there references like DOM previousNode or stuff like that to use in the WHERE statement? Or how could I do this?

Currently I have: ($picId is a php variable)


'(SELECT id, picUri, picUriSmall FROM pictures WHERE id < '.$picId.' ORDER BY id DESC LIMIT 2)
UNION (SELECT id, picUri, picUriSmall FROM pictures WHERE id = '.$picId.')
UNION (SELECT id, picUri, picUriSmall FROM pictures WHERE id > '.$picId.' LIMIT 2)'
This works but doesn't seem to be the best way. I want to be able to have it working for other sorts than by id. And the two first images gotten by this query will be in reversed order (can be fixed in php).

Is there a way to ask for my picIDs position in a query and then use another query to make the LIMIT 10,14 if my image was in position 12?


View Replies !
Paging With Only Next/previous
I'm running PHP + MySQL (innodb) and in some pages, the paging is slowing down the system a lot.

Now I've seen on some sites that have paging implemented in an interesting way. They only let you go forwards and backwards one page at a time, and some don't even tell you how many results there are.

For instance if you look at google search results, they only says "about XXX results". Plus they never actually let you jump to the last page.

I'm thinking of implementing my system this way in certain pages as well, to cut down on the load times. However, I'm exactly sure how that cuts down the load. Can someone shed some light on this?

View Replies !
Selecting Item From Previous Row
Is it possible to get an entry from a previous row in a select statement?

View Replies !
Determine Previous Month
basically I need to display a list of "unpaid" statements for the previous Month, no matter what day the search/request is made on. I know MySQL has the DATE_SUB function where I can subtract 1 Month from the current date, but will that subtract the date exactly 1 month to the day ? Or will it simply give me the Month as I need ?
What happens if the search takes place on the 31st of a month (for example: July), yet the previous month only has 30 days (June). Will it roll back to the previous month correctly or think that the date is 31st June or try to roll back further until it finds another month which has 31 days ?

View Replies !
How To Avoid Previous Results
I want to omit all results(id) from the first query in 2nd query , without using sub query...

View Replies !
Getting Next, Previous Record After INSERT
When I insert a record in a given index order I want to be able to traverse to the previous or next record of that index order, before or after the one I inserted. So for example if I insert a record with an address of "20 Heather Court" using an address index order, I want to be able to push a Next button (on a Web HTML form) to go to the record corresponding to "21 Heather Court" or, if I press the Previous button I
I want to be able to go the record corresponding to "19 Heather Court". Assuming of course that both the Next and Previous records exist before I insert "20 Heather Court".

If I change the index order to customer first name and insert a new record for a customer named "Harry" I then want to be able to push the Next button and see the record for a customer named "Jan" or if I push the Previous button I likewise want to pull up the record for a customer
named "Garry". Assuming that the records for Jan and Garry already existed before I inserted the one for Harry and that they became the Next and Previous records with respect to Harry's after the insertion.

Using an auto increment field does no good because every time I change the index order any kind of sequential relationship between the values in the auto increment field gets totally messed up.

I have also tried statements like " SELECT * FROM Customers WHERE Customer_ID <349 ORDER BY `Customer_ID` ASC LIMIT 0 , 1" without success if there are 10 records before the one with a Customer_ID of 349. A LIMIT of 0,1 will start at record 0 for only 1 record. I want to pull up the record immediately preceding the one with a Customer ID of 349.

In short is there some way to tell which record is Next or Previous to one that I insert under a given index order?

View Replies !
Phase Previous And Next Record
I want do create a "Previous" and a "Next" link, all I have is the ID of the current record. Is there a good way to get the previous and next id of a record with a single SQL Query? example:

ID's: 4,10,7,9,13

I open my page and show all details from the record with the ID 7, now I want to create the links and I need the previous and the next ID, 10 and 9.

I could make a query that returns only the ID's and loop through the recordset till i have my ID's but that's such an ugly solution, not to mention the performance issues.

View Replies !
To Find The Previous Record
I am preparing a ledger using PHP and mysql....I just want to bring the previous closing stock value as the opening stock value of the current transaction.For that i need to find the closing stock value of previous transaction....Is it possible to write a query to find the previous record?

View Replies !
Show Empty Field On Same Previous Value
I have this query now:

SELECT table_2.id,
table_1.naam,
table_2.bedrag
FROM table_2
LEFT JOIN table_1 ON table_2.table_1_id = table_1.id

this is the result:

id naam bedrag
1 pat 200
2 carl 100
3 carl 300
4 bob 500
5 bob 750
6 bob 800

what I want is get the double names replaced by a empty or space:

1 pat 200
2 carl 100
3 300
4 bob 500
5 750
6 800

View Replies !
Total Balance (sum Previous Row With Atual Row)
How to make a select table that give some results like this:

INCOME | OUTCOME | BALANCE
1000000 0 1000000
1000000 0 2000000
500000 0 2500000
0 1000000 1500000

Is it possible?

View Replies !
Compare Previous String In Resultset With Next
The code below does not work.

while(resultSet.next())
{
if (!resultSet.getString.next().equals(resultSet.getString.previous())
{
out.print("...");
}
}

Is it possible to compare previous string in a resultSet with the next string? If so, could you please show me an example.

View Replies !
How To Find Previous Records In The Same Table
how to find previous records in the same table ...

View Replies !
Previous Versions Of MySQL With XAMMP
I've downloaded XAMPP for Linux 1.5.1 for use on Fedora Core 4. I notice it only has the latest version of MySQL on it. I'm working on two web sites (for different clients) and they are hosted on different shared Linux hosting servers (with PHP), each with different versions of MySQL available, i.e:

MySQL 4.0.25-standard
MySQL 3.23.52

Is it possible for me to use older versions of MySQL with XAMPP? Could I just turn off MySQL in XAMMP and just run and older version of MySQL independently of XAMMP? If I can then how would I install multiple versions of MySQL in Fedora Core 4?

View Replies !
Getting Data Entered In The Previous Week
When a product is added to the database it has three seperate fields storing the date added in(01 - 31 format) month (January-December) and year( xxxx).

Is there anyway that I can run a mysql/php query that will just retrieve those products entered within the last xx weeks?

View Replies !
Best Way To Select Next And Previous Items With Order By
to select the previous and next items. I am using this:

DO @a:="title_of_current_item";
SELECT * from tabled where itemtitle < @a order by itemtitle desc limit 0,5; --previous items
SELECT * from tabled where itemtitle > @a order by itemtitle asc limit 0,5; --next items

is there better (one query) and/or faster way?

View Replies !
Get Previous And Next Records Relative To An Index Record
Suppose I have a table "product" with fields id,name,price. I want to
get a resultset of previous and current and next record when the
resultset is sorted by id and id=10. The table data is shown in the
following:

id Name Price
1 : :
5 : :<--------------previous record
10 : :<--------------id=10
12 : :<--------------next record
14 : :

How do I write the sql statement if I do not know 5 and 12 are the
previous and next id respectively?

View Replies !
Unique ID Field, Getting Next And Previous Existing ID From Table
This has been haunting me. In a query I select an ID (the ID field in the table is set to unique) The variable to check against ID is created by user in a form. To prevent unneccesary queries, I'd also like to know what the *next* and the *previous* ID will be.

This is the first query

$results = mysql_query("SELECT * FROM $mytable WHERE ID='$id'");

And now I want to find out what the next ID and previous ID will be from that table. The user can delete records from table, so I can't assume that if user wants a result where ID=8, that records with ID=7 and ID=9 exist.

I hope I explained myself clearly :)

View Replies !
Rolling Averages Each Date But Based On Previous X Days
I have been working on rolling averages and managed to get this working:

Code:....

View Replies !
Carry Forward Column Values From Previous Rows
valuea, valueb, valuec, valued, e, f, g, ....
null, null, null, valued, e, f, g ....
null, null, null, valued, e, f, g ....
valuea, valueb, valuec, valued, e, f, g, ....
null, null, null, valued, e, f, g, ....
etc.

The null values in the first three fields should be replaced with the previous a, b, and c values.

I'm loading the table using load data infile but I can't quite get my head around carrying these values forward. Can I do this with just an SQL statement, perhaps using ifnull(), or do I have to go to perl, or php, or similar?

View Replies !
How To Insert New Records Based On Previous Record Values?
I have
TABLE:

year
value

It has for example,

year, code, vlaue
2007, 33, 4883
2006, 33, 4883
2007, 32, 4883
2006, 32, 4883
2008, 31, 4883
2007, 31, 4883
2006, 31, 4883

I want to insert new records for for 2008 for all codes that do not already have a record for 2008 and increase the value for the 2008 record by 3%.

My several attempts have led to this but which does not work.

UPDATE TABLE a
SET a.value = b.value * 1.05
WHERE b.year = 2007
AND code NOT IN
(SELECT code
FROM TABLE b
WHERE year =2008
AND value !=0)

View Replies !
Current Month
How do you get the current month - 1 month?

(so not a particular day, but the whole month)

View Replies !
Foreach? Add 10 To Current Value
I have a product table. If i want to increase the prices of the items by say $10. what's the syntax for that? How do I get the current value of the field in the current row.

Better explained:

UPDATE table SET price = price + 10?

View Replies !
Current Timestamp
I need to write a technical document for a testing procedure.
one of the steps is to have someone run a query wich will update a specific feild to the current timestamp. I know there is a function in MySQL that does this but im not sure how to use it in a query.
does anyone have a simple way to do this?

update host_users set last_updated = 'current timestamp here' where uname = 'username'

View Replies !
Upgrade From 3.23.49 To Current 5.0 Possible?
is it possible to do this without upgrading by step 3 to 4 4 to 5 ?? or would this totally mess up my current datatbase? which is a simple phpbb2 forum?

View Replies !
How To Get The Current Year
I'm a new bee to Mysql and i want to know how to get the Current year from the system date and then also know how to get the rows which are greater than the fetched year.

View Replies !
Current Users
How can I check what are the current users right now (except for the admin)?
I can't find this in a manual.

View Replies !
Keep Data Current From Another Db
I am using OSCommerce with mySQL as the backend, and I wanted to know what would be the best way to share data (specifically inventory levels) between my Progress db and my mySQL db with minimal or no user interaction?

View Replies !
How To Get Prev And Next Row To Current One?
I have a table called "items" which has following structure and content:

id | name
------------------------------------------
131 | Item Name 1
129 | Item Name 2
128 | Item Name 3
130 | Item Name 4

id field is not auto increment field. The only information I have is:
1. Results must be sorted by name
2. I can fetch current item only by its name e.g "Item Name 3"
3. Can't use offset in this case, as I only know current row by it's name

Is possible to get prev and next row with SQL query (sub-queries maybe?) when I know only 1 row?

View Replies !
Getting Current Date
I am using this to get the date and time to store in a mysql db:

PHP

$date = date("m.d.y"); $time = date("H:i:s");

The field types I am storing them in are date and time format.

Is this the best way to do it? Or are there time stamps that mysql can use automatically?

View Replies !

Copyright 2005-08 www.BigResource.com, All rights reserved