Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 20th, 2005, 11:46 PM
Authorized User
 
Join Date: Aug 2003
Location: , , India.
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #2 (permalink)  
Old January 21st, 2005, 12:54 AM
Friend of Wrox
 
Join Date: Sep 2004
Location: Hyderabad, Andhra Pradesh, India.
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to sureshbabu Send a message via Yahoo to sureshbabu
Default

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

Reply With Quote
  #3 (permalink)  
Old January 21st, 2005, 01:04 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: Chennai, TamilNadu, India.
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi
Default

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

Reply With Quote
  #4 (permalink)  
Old January 21st, 2005, 02:52 AM
Authorized User
 
Join Date: Aug 2003
Location: , , India.
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


regards

Vinay

Reply With Quote
  #5 (permalink)  
Old January 21st, 2005, 04:43 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , United Kingdom.
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

Reply With Quote
  #6 (permalink)  
Old January 21st, 2005, 06:32 AM
Authorized User
 
Join Date: Aug 2003
Location: , , India.
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:06 AM.


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