Reply
match a field between two tables
Old 09-08-2007, 11:18 PM match a field between two tables
Skilled Talker

Posts: 77
I have two table(both in the same database)

tblupdates
updateID(autonumber), upuser, uptitle, upinfo

userlist
username, avatar, password


Then a script that displays the updates.

How can it look at the upuser, then go into the other table, find a username that matches, then response.write the avatar?
Skeddles is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 09-09-2007, 02:53 AM Re: match a field between two tables
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,025
Name: Forrest Croce
Location: Seattle, WA
Select username, avatar from userlist inner join tblupdates on upuser = username

You can add whatever you'd like in a where clause to limit to a specific date, title, or whatever. But rather than creating a routine in your asp code that looks over both tables, let the database do the matching work it's good at. Like Microsoft says, "move the logic to the data, don't move the data to the logic."

If you're trying to build a very high scale application, you might need to cache it, but most of the time you want to delegate to the database.
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 10-10-2007, 08:20 PM Re: match a field between two tables
Skilled Talker

Posts: 77
Code:
<%@ Language=VBScript%>
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Provider = "Microsoft.Jet.OLEDB.4.0"
Conn.ConnectionString = "Data Source=" & Server.MapPath ("datab/db1.mdb")
Conn.Open

Set Rs = Server.CreateObject("ADODB.Recordset")
Rs.Open "SELECT * FROM tblUpdates ORDER BY updatesID DESC inner join tblupdates on upuser = username", Conn, 1,3

rs.MoveFirst

Response.Write rs("upuser")& ":  " & rs("uptitle") & "<br>" & rs("upinfo") & "<br><BR>"
rs.MoveNext
Response.Write rs("upuser")& ":  " & rs("uptitle") & "<br>" & rs("upinfo") & "<br><BR>"
rs.MoveNext
Response.Write rs("upuser")& ":  " & rs("uptitle") & "<br>" & rs("upinfo") & "<br><BR>"
rs.MoveNext
Response.Write rs("upuser")& ":  " & rs("uptitle") & "<br>" & rs("upinfo") & "<br><BR>"
rs.MoveNext

set Rs = nothing
set Conn = nothing


%>

thats my code.

how do i write the avatar depending on who the user is?
Skeddles is offline
Reply With Quote
View Public Profile
 
Old 10-11-2007, 03:43 AM Re: match a field between two tables
chrishirst's Avatar
Super Moderator

Posts: 11,895
Location: Blackpool. UK
rs("avatar_column") inserted into a <img> element? more info on the table structure would help

"ORDER BY criteria" should be last in the SQL statement.

don't use "SELECT * FROM" it is very inefficient
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-11-2007, 04:53 PM Re: match a field between two tables
Skilled Talker

Posts: 97
Name: Ganesh
skeddles,

Can't resist to notice this bug:

Rs.Open "SELECT * FROM tblUpdates ORDER BY updatesID DESC inner join tblupdates on upuser = username", Conn, 1,3

Changed to:

Rs.Open "SELECT * FROM tblUpdates inner join userlist on upuser = username ORDER BY updatesID DESC", Conn, 1,3

Avoid select (*) like chris mentioned, use the needed column names in select query.
__________________
Gather. Search. Compare. Save on Hotel and Flight Prices @ www.vtrip.info
sri_gan is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to match a field between two tables
 

Thread Tools

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

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




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13220 seconds with 13 queries