Avatar billede guzzie Nybegynder
28. maj 2001 - 09:36 Der er 10 kommentarer og
1 løsning

search results from 2 tables

I have a search script that I would like to search thru 2 tables and not just the one as it does not what is the best way to do this. As my script stands know it looks like this
    strSQL = \"SELECT * FROM listall WHERE\"
    strSQL = strSQL & \" ( name LIKE \'%\" & strKeyword & \"%\')\"
    strSQL = strSQL & \" OR (descript LIKE \'%\" & strKeyword & \"%\')\"
    strSQL = strSQL & \" OR (info LIKE \'%\" & strKeyword & \"%\')\"
    strSQL = strSQL & \" OR (example LIKE \'%\" & strKeyword & \"%\')\"

    strDSN = \"DRIVER={Microsoft Access Driver (*.mdb)};DBQ=\"&Server.MapPath(\"../db/media.mdb\")
    Set myConn = Server.CreateObject(\"ADODB.Connection\")
    myConn.Open strDSN

    Set rs = myConn.Execute(strSQL)
    If Not (rs.BOF Or rs.EOF) Then

    Do While Not rs.EOF
        Response.Write \"<tr><td valign=top align=left rowspan=2><a href=vis_data_alt.asp?id=\" & rs(\"Id\") & \" ><img border=0 width=17 height=17 src=icons/ret.gif alt= Læs&nbsp;mere&nbsp;om&nbsp;\" & rs(\"name\") & \" ></a></td><td width=520 valign=top align=left><b><a href=vis_data_alt.asp?id=\" & rs(\"Id\") & \" > \" & rs(\"name\") & \" </a></b></td></tr>\"
        Response.Write \"<tr><td id=td_noborder_color> \" & rs(\"info\") & \"<br><br></td></tr>\"
        rs.MoveNext
    Loop

I changed
    strSQL = \"SELECT * FROM listall WHERE\"
to
    strSQL = \"SELECT * FROM listall, articals WHERE\"
articals is the other table, this of course doesnt work but am I on the right track ??
And how would one call the other columes from the other table ?? I have seen the question here on Eksperten before but just cant seem to find it
Any help would be great
//Guzzie
Avatar billede tofte Juniormester
28. maj 2001 - 09:54 #1
Are the structure in the two tabels identical, do the tabels contaion the same fields?
Avatar billede nuno Nybegynder
28. maj 2001 - 09:55 #2
strSQL = \"SELECT * FROM listall, articals WHERE\"
    strSQL = strSQL & \" ( listall.name LIKE \'%\" & strKeyword & \"%\')\"
    strSQL = strSQL & \" OR (listall.descript LIKE \'%\" & strKeyword & \"%\')\"
    strSQL = strSQL & \" OR (listall.info LIKE \'%\" & strKeyword & \"%\')\"
    strSQL = strSQL & \" OR (listall.example LIKE \'%\" & strKeyword & \"%\')\"


if you would like to call the columns from the other table you should add similar LIKE clauses just using articals.Column where Column is the name of the column you would like to refer to.

/nuno
   
Avatar billede tofte Juniormester
28. maj 2001 - 09:55 #3
contaion=contain
Avatar billede guzzie Nybegynder
28. maj 2001 - 10:00 #4
hey there
//tofte no the tables contain diffrent names
I found an example in asp for dummies but that requiered the 2 table to have the same names

//nuno I will test your idea out

but in a couple of minutes :)
Guzzie
Avatar billede guzzie Nybegynder
28. maj 2001 - 10:09 #5
oops How do I set the connection ??
it looks like this now
strDSN = \"DRIVER={Microsoft Access Driver (*.mdb)};DBQ=\"&Server.MapPath(\"../db/media.mdb\")
    Set myConn = Server.CreateObject(\"ADODB.Connection\")
    myConn.Open strDSN

    Set rs = myConn.Execute(strSQL)
    If Not (rs.BOF Or rs.EOF) Then

    Do While Not rs.EOF
        Response.Write \"<tr><td valign=top align=left rowspan=2><a href=vis_data_alt.asp?id=\" & rs(\"Id\") & \" ><img border=0 width=17 height=17 src=icons/ret.gif alt= Læs&nbsp;mere&nbsp;om&nbsp;\" & rs(\"name\") & \" ></a></td><td width=520 valign=top align=left><b><a href=vis_data_alt.asp?id=\" & rs(\"Id\") & \" > \" & rs(\"name\") & \" </a></b></td></tr>\"
        Response.Write \"<tr><td id=td_noborder_color> \" & rs(\"info\") & \"<br><br></td></tr>\"
        rs.MoveNext
    Loop

normally I would call like this rs(\"info\")
how ever I get an error saying that it is invalid
any thoughts
//Guzzie
Avatar billede nuno Nybegynder
28. maj 2001 - 10:19 #6
rs.open strSQL, myConn

then you open your recordset with the connection you have opened - and with the sql clause, that you want to use.

/nuno
Avatar billede guzzie Nybegynder
28. maj 2001 - 10:28 #7
hi there
im still a little stuck ;(
I changed    myConn.Open strDSN
to
rs.open strSQL, myConn
is this correct ??
sorry if im taking a little long with it but I havnt tried this before ;(
Guzzie
Avatar billede nuno Nybegynder
28. maj 2001 - 10:43 #8
no - you shall still use

myConn.open strDSN

- that was to open your db-connection.

But you must ALSO use:
rs.Open strSQL, myConn

to open your recordset.

/nuno
Avatar billede guzzie Nybegynder
28. maj 2001 - 12:23 #9
hmmm Im still trying to get it to work im still getting an error message :(
Avatar billede nuno Nybegynder
28. maj 2001 - 12:32 #10
what exactly is the error message?
Avatar billede guzzie Nybegynder
28. maj 2001 - 13:30 #11
sorry about the :)
it works now it just took me a little time to work it out
but you are quite right it works fine :)

thanks very much :)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester