 |
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

January 20th, 2005, 11:46 PM
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Problem in query
Hi all !
I have two tables
Users Table:
UserId(Primary Key)
username
fname
email
Photos table:
photoid (Primary Key)
UserId (ForeignKey)
photoname
isactivated
In the photos table each user may have records varying from 1 to 3 (i.e max 3 photos).
But i need a query to retrieve a single record of each user from photos as well his username,fname and email from Users table.
Code:
select distinct (Photos.UserId),photoname,isactivated,username,fname,email from
photos,users where photos.UserId=users.UserId
The above query doesn't work and fetches multiple photos of each user
as each record happens to be distinct because the photoname in all of them is different.
I need each users sigle photo record along with his user table details.
Thanks in advance.
Vinay
|

January 21st, 2005, 12:54 AM
|
Friend of Wrox
|
|
Join Date: Sep 2004
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
Here you can do this in two ways..
1. You can use you logic in front end in the following way..
Get the userid into a varaible and compare the user Id of next useri with this variable and if it is same then skip the furthur process like displaying a Photo...
2. If you have only One photo as activated ( i.e isactivated =1 ) Then
you can use following query..
select Max(Photos.photoid ),photoname,isactivated,username,fname,email from
photos,users where photos.isactivated=1 Group by UserId.
Thanks
Suresh
|

January 21st, 2005, 01:04 AM
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
If you want each users single photo record you need to check the photoid also in the where condition.
|

January 21st, 2005, 02:52 AM
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry guys!
I haven't found any correct solution yet !!
Still waiting for the right solution.
regards
Vinay
|

January 21st, 2005, 04:43 AM
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hi Leo,
This should do it...
Code:
SELECT DISTINCT u.UserId, u.username, u.fname, u.email, p.photoname, p.isactivated
FROM users u INNER JOIN photos p ON u.UserId = p.UserId;
HTH,
Chris
|

January 21st, 2005, 06:32 AM
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by ChrisScott
Hi Leo,
This should do it...
Code:
SELECT DISTINCT u.UserId, u.username, u.fname, u.email, p.photoname, p.isactivated
FROM users u INNER JOIN photos p ON u.UserId = p.UserId;
|
Chris thanks for the suggestion. But it doesn't solve the problem.
I also did it the same way. The reason it doesnt work is when you use distinct in the join condition then the distinct clause operates on the record level and not on the field level.
So here it will bring multiple records of the same person.
That is more than one record will be fetched from the photos table having the same UserID. Though the UserId are same but the other combination like the "photoname" make the records distinct from each other...hence no effect of the "DISTINCT" clause.
Anyways i found a way out...may be not that good,but it is effective.
select u.userid,u.username,u.email,p.photoname,p.isactiva ted from users u, photos p where u.userid=p.userid and p.id in (select max(id) as id from photos group by userid)
regards
Vinay
|
Similar Threads
|
Thread |
Thread Starter |
Forum |
Replies |
Last Post |
Query Problem. |
rupen |
Access |
3 |
April 27th, 2007 07:43 AM |
Query Problem |
bundersuk |
VB Databases Basics |
0 |
December 30th, 2006 07:50 AM |
query problem |
purnendu2311 |
Other Programming Languages |
2 |
December 1st, 2006 02:53 PM |
problem with query |
harpua |
Classic ASP Databases |
1 |
January 24th, 2005 12:36 PM |
query problem |
mateenmohd |
SQL Server 2000 |
7 |
September 9th, 2003 11:58 PM |
|
 |