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.





Selecting Item From Previous Row


Is it possible to get an entry from a previous row in a select statement?




View Complete Forum Thread with Replies
Sponsored Links:

Related Messages:
Selecting Item List And Count From Other Table
I have been failing at trying to get this query to work. Here is the query I can't get to work:

SELECT cont.key_no, (
SELECT COUNT(1)
FROM items itm
WHERE itm.cont_key_no = cont.key_no ) AS item_count
FROM containers cont
WHERE cont.status = 'OPEN';

Here is a query I managed to get working, but it doesn't quite do what I want it to. It doesn't return a container and item count for containers that don't have items:

SELECT cont.key_no, COUNT(itm.key_no) AS item_count
FROM containers cont, items itm
WHERE cont.status = 'OPEN'
AND itm.cont_key_no = cont.key_no
GROUP BY cont.key_no

Here are what the tables look like:

containers
----------
key_no
status

items
---------
key_no
cont_key_no

If you can't tell from the examples above, I am trying to return a list of all 'OPEN' containers and how many items are in the container, even if no items are in the container.

View Replies !   View Related
Selecting All Items From Table1 Where It Does Not Have An Associated Item In Table2
I have a table, menu_items, with id, service_parameter, and text. I have another table, menu_item_parents, with parent_id and child_id, so that an item might have multiple parents and multiple children.
Now I want to select all top level items. It seemed like it'd be simple enough, but I can't figure it out. How owuld you select all items in menu_items that does not have any rows in menu_item_parents where child_id = the menu item id?

View Replies !   View Related
Side Menu Item Invisible Unless MySQL Contains Item
I have a menu attached to a MySQL DB that has various items stored and are called with PHP when a user clicks on the link. (I included the menu code below)

I want to add a menu item that is only visible if there is something stored in the DB.
I bolded the area below as to the part of the menu I want the field. Can someone please show me how to incorporate this in that general area with PHP? I have scoured the Web and Googled but could not find a way to do this. Code:

View Replies !   View Related
Item=item+1
I have the following query going:

$query = mysql_query("UPDATE sites SET hitcount=hitcount+1 WHERE siteid='".$siteid."' ");

but instead of adding 1, it adds 2, every time. I'm on MySQL 4.1.18. What am I doing wrong?

View Replies !   View Related
Item Before Another
I need to put an item before another and they are ordered by Id which is an autoincrement column, how do i do it?

mysql_query("REPLACE table WHERE a = $a SET id = $newid ...then?
how do i tell mysql to add a 1 to all the other id which are greater then the one i just updated?

View Replies !   View Related
Put Item First
Hello, i have a basic (?) problem, i have a long list of data sorted:

abcdefghijklmn

I'd like to take one 'f' and move in front:

fabcdeghijklmn

And do that over and over

How do i do that in a way i can traverse the list, item by item. Do I have to do a linked list (risking pointer trouble etc)?

I'm using PHP and Mysql, the data will be maybe 0.5 - 10 million rows.

View Replies !   View Related
Adding An Item
I don’t know how to add one new item in ENUM field. (Is it possible anyway? )
Any hint.

View Replies !   View Related
SELECT Item
I can't seem to get this although I know it's probably simple. I need to SELECT the text in a specific col, but just the text next to the largest auto_increment number.Code:

id | col_text
_______|__________

1 | text A
2 | text B
3 | text C
4 | text D
... ... etc

The thing is, I won't know what the actual number is in the id col, i need a way to select the text in the record with the largest id number.

View Replies !   View Related
Related Item
im currently working on a simple lyric page:

I have 2 data : title and keyword

when a user view a particular lyric, i want a related lyric by title & keyword to be display

example data in database

title
Green Day Walking Alone

Keywords(separated by commas)
Green Day, walking alone, tabs, midi

i use this code below but the result display only one title which is the same title as the currently viewed lyric

SELECT title, keywords FROM lyrics
WHERE title LIKE '%".$arr[title]."%'
AND keywords LIKE '%".$arr[keywords]."%'

how to fix this is my sql format correct ?

View Replies !   View Related
Setting Up New Item
I use myphpadmin and I am a beginner with it. I have this text based game and for items it's weapon, armor and shield.

I went to insert and did the fields and added a new item called "Herb" which heals your character 10 health. So now I go to the game to the admin control panel and its there but it can only be a weapon, armor or shield. I need to add a new class for "items" which is a healing item.

View Replies !   View Related
Item With Multiple CatIDs
I have just started a new database, and in it I have a table of items. One of the fields is 'categories' which relates to another table of category IDs, but some items will have more than one category ID.
Whats the best way of doing this? And how do I select data? eg. SELECT all items WHERE catID = 4
SELECT all items GROUP BY catID
SELECT item 56 WITH category data.

View Replies !   View Related
Returning Top 5 Tags For An Item
System Info:
RedHat Linux 9 (text only)
Mysql 3

When I execute mysql from the command line I receive an error:

"error 2002: Mysql cannot connect to socket /var/lib/mysql.sock"

(or something similar) - the socket was definately not
"/tmp/mysql.sock"

It was installed by the RedHat 9 installer. Do I have to edit some
configuration file.

View Replies !   View Related
How To Search All Fields For An Item?
I currently use a query like:

SELECT * FROM sr_main WHERE footprint LIKE 'conn%0p80%';

What I would like to be able to do is search ALL record fields rather than searching just the field 'footprint'.

There must be an easy way!?

I thought this would work:

SELECT * FROM sr_main WHERE * LIKE 'conn%0p80%';

but it seems I can't wildcard the fields... is that right?

View Replies !   View Related
Apply SHA() To Every Item In Column?
I have a password column with the passwords already set for every user. Is there a simple statement I can use to apply the SHA() encryption function to every value in the password column in my table users?

View Replies !   View Related
Inserting An Item Into 2 Tables
I have a simple database with 2 tables. Table 1 = Product Info, and Table 2 = Product Stats. I could have it all in one table, but I use two tables to keep things organized. I have a unique ID that ties the two tables together.

Now, I have a little problem adding a new product to my database (with a php script). This is what I have:

mysql_query("INSERT INTO products(name,description,price,size,weight) VALUES(''$name','$desc','$price','$size','$weight') ")or die(mysql_error());mysql_query("INSERT INTO stats(popularity,sales,opinions) VALUES('','','') ")or die(mysql_error());

The problem is that sometimes when I add a new product, the ID will be different in each table. For example, I just added a new product, and the ID in table "products" was 114 while the ID in table "stats" was 115. This of course causes problems, each product has to have the same ID on both tables.

Is there a better query I could use? The products table always has info inserted, but the stats tables starts-off with all values being empty (they get populated with time, as sales start, etc)

View Replies !   View Related
Select Items With Same Catehory As Given Item
I have two tables:

TABLE_ARTICLE
article_id******article_name article_content

TABLE_ARTICLE_SECTION
article_id *section_id


Given an article_id, how do I select all articles from
TABLE_ARTICLE that have the same section_id as that of a given
article_id.

Something like this but for mysql:
SELECT table_article.* FROM table_article,table_section WHERE
table_section.section_id IN (SELECT section_id FROM table_section
WHERE article_id="something")

View Replies !   View Related
How To Create A Query To Get Top 5 Item For Each Date
I have running out of idea on hw to create a query to get each day top 5 item with a date range given more than 1 month or more.

meant: if i have a date range from 2006-01-23 to 2006-10-23, then i want to get the latest 31 days data only with each day TOP 5 item. How?

Lets say i have a table with this keyword: MyDate;Item;CountItem;

The date range is unknown and can be any range selected by user.

I am trying to think of put auto increament for each day with CountItem desc and each day the column of auto-increament is set to 5 only so at last i can just get all the data from it. But the question is i dunno how to do it?

Anyone have better solution? I know if using store procedure then can do this but is it can done without store prodedure?

View Replies !   View Related
Select More Than One Item From Column In Table
I'm trying to select a few items in a table.

Select * from table where column1 = 'a' and 'b';

I have a feeling its either the '=' or the 'and' bit at the end of the query statement.

I've tried the 'or' and 'like' and running out of ideas and the will to live.

View Replies !   View Related
Displaying Item In Favourite List
i have save user's favourite posting in to database..

the record is saved in the format "1,3, 15"

when user adding a new posting id (eg.10) to the favourite list,the record become "10, 1,3, 15"

i'm having problemin displaying the favourite list of the user.

how can i display the item in the below format?

id:10
title:
author:

id:1
title:
author:

id:3
title:
author:

View Replies !   View Related
Obtain New Id Of New Item Added To Mysql Table
I wish to find out how i can get the id of the most recent item added to the mysql database

The ID is set by the mysql database by increasing by one to the last entry.

I need to find the best way to find out what this number is.

View Replies !   View Related
Table Design For Storing Item Stats
I've created quite the database for my app, and I'm familiar with normalization and general db design. However, I'm ready to design my tables for recording all sorts of stats for my db items.

I want to be able to view stats for each item, such as number of small views, number of large views, number of downloads, ratings and so on. I also want to be able to view these items by periods of time, such as by day, week, month, by multiple months, by year, etc. So, I'm trying to figure out what the most efficient way of recording this data, so that I can keep my tables as small as possible. I'm wondering if I will have to insert a row for every stat, or if I should enter a row for every day, or only a day if there is activity, etc.

View Replies !   View Related
Browsing Records: (first, Prev, Next, Last) And Getting A List # Like Item # Of ##
Very simple database to store comics, I'm trying to start small with PHP and MySQL.

table name: comics
Fields:
ID: int auto-increment primary-key
title: tinytext
date: date
location: text (stores the relative file location, "images/comics/1.jpg")


So now my trouble...

In a perfect world, the database ID field would one day have 100 records with ID numbers 1-100 consecutively. In the real world I see myself having to delete entries for whatever reason which will leave gaps in the ID field.

Is there a mysql command to go to the next entry that comes after a certain ID number? Same with previous.

Is there a way to find the highest number ID in the database quickly? The lowest?

Finally, I would like to display something such as "Comic X of XX". XX is easy, I can just use count. But if I retrieve ID 22, how do I know how many records came before it without doing something like looping through the database in PHP and running query for each preceeding ID#? I assume that PHP loop method would be time consuming and a poor use of resources? Or would that actually make sense?

Sorry for the newb questions. The book I bought was more reference than tutorial, I didn't have enough time to pick one out properly which leaves me floundering a little with the basics.

View Replies !   View Related
Elt Command - Extracting Single Item From Group From Field
I have a varchar field which contains a comma delimeted set of data. I
am trying to us the ELT function to extract the first part of the group.
Problem is, ELT does not seem to work when the data comes from a field.
If I take the data and do,

select elt(1,"red","green","blue")
RESULT would be "red"

But if the data was stored in a field (called group) and I did:

select elt(1,group)
RESULT would be red,green,blue

Why is this a problem? I am running mysql 4.1.0 on windows. I am going
to try this on another / older / newer version of a DB today and see if
I can replicate the problem. Wandering if this is a bug or not?

Also, does anyone know if there is a function that compares sets
together. Similar to find_in_set, except both the needle and the
haystack are sets.

View Replies !   View Related
Set A Data Item Value Depending On A Generated Record Number
how to SELECT a running total of the records being returned from a query when the line number is not provided.

I am downloading product lines belonging to an order from a table using
Expand|Select|Wrap|Line Numbers

View Replies !   View Related
When A New Item Is Added I Need To Make Sure That The ItemID Is Correct In Each Table
If i have 2 tables

table link
PageID, ItemID


table Item
ItemID, Data

each Item is linked to a page and a page can have 1 or more Items

When a new item is added I need to make sure that the ItemID is correct in each table, how do I insure this. Should I make Item ID auto increment in each table starting at 1 and make sure that Items are removed at the same time upon deletion?

View Replies !   View Related
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 !   View Related
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 !   View Related
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 !   View Related
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 !   View Related
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 !   View Related
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 !   View Related
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 !   View Related
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 !   View Related
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 !   View Related
Most Efficient Way To Create Drop Down Boxes With Item Count Using MySQL
[Back Type: All]
[Hardback (100)]
[softback (400)]

and

[Genre: All]
[Biography (94)]
[Crime (244)]
[Horror (14)]
[Romance (148)]

View Replies !   View Related
Joins Across Multiple Tables, Influence Which Item To Take If Only One Is Needed Of Each Kind
I'm trying to get a join across multiple tables to work where I have to walk across several pivot tables to get one of the selection criteria.

What I want is the latest entries for each supplied wp_terms (the category) in the wp_posts table. While fetching each one with its own query is easy, I'm stumbling when trying to build a query that returns the latest entry for a list of supplied categories. Group by doesnt seem to cut it, since it takes place before the order by.

Here is the query and its output (without grouping, this will return all entries for the specified categories): ....

View Replies !   View Related
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 !   View Related
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 Replies !   View Related
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 !   View Related
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 !   View Related
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 !   View Related
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 !   View Related
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 !   View Related
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 !   View Related
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 !   View Related
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 !   View Related

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