p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Classic ASP Databases (http://p2p.wrox.com/forumdisplay.php?f=62)
-   -   Problem in query (http://p2p.wrox.com/showthread.php?t=24248)

leo_vinay January 20th, 2005 11:46 PM

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

sureshbabu January 21st, 2005 12:54 AM

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


Santhi January 21st, 2005 01:04 AM

If you want each users single photo record you need to check the photoid also in the where condition.


leo_vinay January 21st, 2005 02:52 AM

Sorry guys!
I haven't found any correct solution yet !!
Still waiting for the right solution.


regards

Vinay


ChrisScott January 21st, 2005 04:43 AM

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


leo_vinay January 21st, 2005 06:32 AM

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



All times are GMT -4. The time now is 12:56 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.