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

Pulling the latest values from a table

I am not a mysql expert and was trying to find something like this.

I have two tables.
Comments Table has 3 fields, country, comments and date with NO unique or primary keys

Table below
_____________________
country comments date
one ddd 10/1/2007
two eee 1/1/2007
three aaa 8/1/2007
one aaa 10/11/2007
two qqq 1/11/2007
three www 1/5/2007
one fff 1/3/2007
two rrr 1/8/2007
three yyy 7/1/2007


User Table has 2 fields, user and country. again no unique or primary fields
User Table Below
_____________
user country
jack one
tom three
jack three

I would like to do this.
Display Just two LATEST comments from the comments table for EACH User.
For example with the above query and tables, i would see something like this as a result, or at least expect it.

jack one ddd 10/1/2007
jack one aaa 10/11/2007
tom three aaa 8/1/2007
tom three yyy 7/1/2007

Any help would be much appreciated. Thank u.
Oct 18 '07 #1
1 1203
pbmods
5,821 Expert 4TB
Heya, bostonguy70. Welcome to TSDN!

With no unique/primary keys, it's pretty tough to do this efficiently.

Looks like something along the lines of:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `users`.`user`,
  3.         `users`.`country`,
  4.         `comments`.`comments`,
  5.         `comments`.`date`
  6.     FROM
  7.     (
  8.             `comments`
  9.         LEFT JOIN
  10.             `users`
  11.                 USING (`country`)
  12.     )
  13.     WHERE
  14.     (
  15.         `comments`.`date`
  16.         IN
  17.         (
  18.             SELECT
  19.                     `sub`.`date`
  20.                 FROM
  21.                     `comments` AS `sub`
  22.                 WHERE
  23.                     `sub`.`country` = `users`.`country`
  24.                 ORDER BY
  25.                     `date` DESC
  26.                 LIMIT 2
  27.         )
  28.     )
  29.     GROUP BY
  30.         `users`.`country`,
  31.         `comments`.`date`
  32.  
Not sure how you'd want to report multiple Users per country; this query more-or-less randomly truncates results so that you're only fetching two records per country. You can also allow for unique Users by adding it to the GROUP BY clause.
Oct 18 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: TG | last post by:
I have a problem trying to get a value from textbox 1 and textbox 2. If those two values match the data in the database then it has to return a third corresponding value to a third textbox (or...
5
by: alanspamenglefield | last post by:
Hello group, I have an SQL statement which pulls data from a table as follows: " SELECT tblSites.sites_siteno, " & _ " tblSites.sites_sitename, " & _ " Sum(tblStockResults.stkr_result) AS...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
8
by: TGEAR | last post by:
I have an ItemSTDPriceHistory table as below and this is a child table of itemlookup table with one to many relationship. if exists (select * from dbo.sysobjects where id = object_id(N'.') and...
4
by: Mark | last post by:
Hey folks, I'm looking at making the following query more efficient potentially using the ranking functions and I'd like some advice from the gurus. The purpose of the following is to have a...
2
by: julie18881 | last post by:
I may be being really stupid here, i have spent the last 3 hours looking round your site and some other for answers to my problem, but have not had much luck (possibly cuase my brain just isn't...
1
by: varmamkm | last post by:
Hi, I hava a table with the following information CREATE TABLE TEMP1 (REFID INT, REVISION INT, FIELDNAM VARCHAR(10), VALUE VARCHAR(10)); INSERT INTO TEMP1 VALUES(1001, 0, 'A', 'A2'); INSERT...
0
by: Daniel Roy | last post by:
Here's what I came up with (sorry if it doesn't exactly match your example, but I could only work with the info you provided): SQLcreate table latest (person_id number not null, date_entry...
12
by: Alexnb | last post by:
This is similar to my last post, but a little different. Here is what I would like to do. Lets say I have a text file. The contents look like this, only there is A LOT of the same thing. () 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: 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: 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...
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
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
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
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,...
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...

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.