473,396 Members | 1,755 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,396 software developers and data experts.

I'm stuck searching for a query

Hi !

I've got a problem trying to figure out a query.

I have a table with following fields:

tool varchar(30)
last_date datetime
user_id mediumint(8)
....

example:

tool last_date

1 mail 2004-09-05 12:11:44
2 mail 2004-09-01 11:33:12
3 agenda 2004-09-06 17:34:55
4 export 2004-09-02 14:33:16
5 agenda 2004-08-30 19:55:32
6 mail 2004-09-07 14:13:12
7 agenda 2004-09-02 09:54:35

Now i'm trying to creat a query that gives me the tools who were last changed.

this should give:

6 mail 2004-09-07 14:13:12
3 agenda 2004-09-06 17:34:55
4 export 2004-09-02 14:33:16

If anyone can help me with this query, it would be greatly appreciated.

Thanx.
Jul 20 '05 #1
2 1395
Select * From tblname Order By last_date

Patrick

sc**********@hotmail.com (Olivier) wrote in message news:<ac**************************@posting.google. com>...
Hi !

I've got a problem trying to figure out a query.

I have a table with following fields:

tool varchar(30)
last_date datetime
user_id mediumint(8)
...

example:

tool last_date

1 mail 2004-09-05 12:11:44
2 mail 2004-09-01 11:33:12
3 agenda 2004-09-06 17:34:55
4 export 2004-09-02 14:33:16
5 agenda 2004-08-30 19:55:32
6 mail 2004-09-07 14:13:12
7 agenda 2004-09-02 09:54:35

Now i'm trying to creat a query that gives me the tools who were last changed.

this should give:

6 mail 2004-09-07 14:13:12
3 agenda 2004-09-06 17:34:55
4 export 2004-09-02 14:33:16

If anyone can help me with this query, it would be greatly appreciated.

Thanx.

Jul 20 '05 #2
> Now i'm trying to creat a query that gives me the tools who were last changed.

This is one of those types of problem queries that folks post regularly
to this newsgroup: how does one output a group-invariant value _and_
row-specific values in one query?

One method is to use subqueries:

select t1.user_id, t1.tool, t1.last_date
from myTable t1 inner join
(select tool, max(last_date) as max_last_date
from myTable group by tool) t2
on t1.tool = t2.tool and t1.last_date = t2.max_last_date

Note that subqueries require MySQL 4.1. This example won't work in
MySQL 4.0 or earlier.

Another method is to fetch all the rows with a simpler query, and
calculate the max date in your application code. For example, in
Perl/DBI this might be:

my $st = $dbh->prepare('select user_id, tool, last_date from myTable
order by last_date');
$st->execute();
my $result_set = $st->fetchall_arrayref({});
my %tool_hash = ();
for my $row (@$result_set)
{
$tool_hash{ $row->{tool} } = {
user_id => $row->{user_id},
last_date => $row->{last_date}
};
}

This overwrites the hash list entry for each tool as it iterates through
the result set, and since the query was ordered by last_date, it should
naturally produce a final %tool_hash containing the most recent values
per tool.

Note these code examples are untested.

Regards,
Bill Karwin
Jul 20 '05 #3

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

Similar topics

4
by: donald | last post by:
Hi all, I have a website running asp (about to move to asp.net soon though) which has a list of DVD's I have the various pages I want, last 10, listing, full listing ect, but the one page i can't...
3
by: aaj | last post by:
This is a simple question compared to some of the stuff that gets asked (and answered) here, but sometimes its easy to over look the simpler things. I've been working with databases for a few...
2
by: Alex A via .NET 247 | last post by:
Hello all. I'm new to .NET and I am stuck and I just cannot getthis to work (even after much searching). I have a listbox populated by data from an Access Database. Themanner in which I fill the...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
1
by: Robert Oschler | last post by:
I read a while back that MySQL will only use one index per query. (If this is not so, please tell me and point me to a doc that gives a good explanation of MySQL's current index usage policy). ...
4
by: Jordan S. | last post by:
Using .NET 2.0 (C#) I'm writing a small app that will have a "Person" class that exposes FirstName and LastName properties. At runtime, a "People" collection will be populated with a few thousand...
1
by: colleen1980 | last post by:
When I run my query it stuck is it possible that i can debug it SELECT TOP 1 tblCredits.OPR, Min(tbl_CRCDollars.SumOfAMOUNT) AS MinOfSumOfAMOUNT, Avg(tblCredits.Credit) AS AvgOfCredit,...
8
by: Allan Ebdrup | last post by:
What would be the fastest way to search 18,000 strings of an average size of 10Kb, I can have all the strings in memory, should I simply do a instr on all of the strings? Or is there a faster way?...
3
by: Aaron | last post by:
I'm trying to parse a table on a webpage to pull down some data I need. The page is based off of information entered into a form. when you submit the data from the form it displays a...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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...
0
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...

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.