Prova?
Du kan ha hur många like frågor du vill.
Fast:
username like '%" & request.form("1") & "%' and username like '%....
Databaser & SQLur Databashanterare & SQL
27 svar · 1 201 visningar · startad av SGuncel
Hej
Jag håller på göra avancerat sökning funktion. Jag undrar om man kan ha cirka 6 olika Like på en SQL Sats?
Typ så här:
Set ObjRS = ObjConn.Execute("SELECT * FROM tblusers WHERE userName LIKE '%"&SafeSQL(Request.Form("userName"))&"%' and '%"&SafeSQL(Request.Form("userlive"))&"%' and '%"&SafeSQL(Request.Form("userage"))&"%' and '%"&SafeSQL(Request.Form("userName"))&"%' and '%"&SafeSQL(Request.Form("userSex"))&"%' and '%"&SafeSQL(Request.Form("userPic"))&"%'" & " order by userName")
Prova?
Du kan ha hur många like frågor du vill.
Fast:
username like '%" & request.form("1") & "%' and username like '%....
Flyttas från ASP
Nu så har jag kommit så här långt:
<form action="?do=search" method="post">
<div style="font-family: Tahoma; font-size: 12px;">
<input type="text" name="userName" style="padding:1px; font-family: Tahoma; font-size: 12px; border: 1px dotted #B0B0B0">
<input type="radio" name="userSex"> Alla
<input type="radio" name="userSex" value="Flicka"> Flickor
<input type="radio" name="userSex" value="Kille"> Killar
<input type="checkbox" name="userPic" value="1"> Med foto
</div>
<div style="font-family: Tahoma; font-size: 12px;">
Från
<SELECT name="userAge" style="padding:1px; font-family: Tahoma; font-size: 12px; border: 1px dotted #B0B0B0">
<option value=""></option>
<%
for i = 1990 to 1966
%>
<option value="<%= i %>"><%= i %></option>
<%
next
%>
</SELECT>
Till
<select name="userAge2" style="padding:1px; font-family: Tahoma; font-size: 12px; border: 1px dotted #B0B0B0">
<option value=""></option>
<%
for i = 1990 to 1966
%>
<option value="<%= i %>"><%= i %></option>
<%
next
%>
</select>
<%
Set ObjRS = MinConn.Execute("SELECT * FROM Orter WHERE OrtNamn order by OrtNamn")
%>
Bor
<select name="userLive" style="padding:1px; font-family: Tahoma; font-size: 12px; border: 1px dotted #B0B0B0">
<option value=""></option>
<%
Do until ObjRS.Eof
%>
<option value="<%=ObjRS("OrtNamn")%>"><%=ObjRS("OrtNamn")%></option>
<%
ObjRS.Movenext
Loop
ObjRS.Close : Set ObjRS = Nothing
MinConn.Close : Set MinConn = Nothing
%>
</select>
</div>
<br />
<div style="font-family: Tahoma; font-size: 12px;">
<input type="submit" name="search" style="padding:1px; font-family: Tahoma; font-size: 12px; border: 1px dotted #B0B0B0" value=" Sök ">
</div>
</form>
<br />
<%
IF Request.Querystring("do")="search" then
Set ObjRS = ObjConn.Execute("SELECT * FROM tblusers WHERE userName LIKE '%"&SafeSQL(Request.Form("userName"))&"%' and userSex LIKE '%"&SafeSQL(Request.Form("userSex"))&"%' and userPic LIKE '%"&SafeSQL(Request.Form("userPic"))&"%' and userAge LIKE '%"&SafeSQL(Request.Form("userAge"))&"%' and userLive LIKE '%"&SafeSQL(Request.Form("userLive"))&"%'" & " order by userName")
IF ObjRS.Eof then
Response.Write "Inga medlemmar hittades."
else
%>
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="table6">
<tr>
<td style="width:25%;">Användarnamn</td>
<td style="width:25%;">Kön</td>
<td style="width:25%;">Ålder</td>
<td style="width:25%;">Bor</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="table7">
<tr>
<td width="100%" style="border-top: 1px solid Silver;"><img src="../_Grafik/Pix.gif" width="0" height="1"></td>
</tr>
</table>
<%
Do until ObjRS.Eof
%>
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="table5">
<tr>
<td style="width:25%;"><% Response.Write "<a href=""profil.asp?userId="&Server.HtmlEncode(ObjRS("userId"))&""">"&Server.HtmlEncode(ObjRS("userName"))&"</a>" %></td>
<td style="width:25%;"><% Response.Write ""&Server.HtmlEncode(ObjRS("userSex"))&"" %></td>
<td style="width:25%;"><%=Age(DateSerial(ObjRS("userAgeYear"), ObjRS("userAgeMonth"), ObjRS("userAgeDay")))%> år</td>
<td style="width:25%;"><% Response.Write ""&Server.HtmlEncode(ObjRS("userLive"))&"" %></td>
</tr>
</table>
<%
ObjRS.Movenext
Loop
ObjRS.Close : Set ObjRS = Nothing
ObjConn.Close : Set ObjConn = Nothing
End if
End if
%>
Hur gör jag på SQL satsen för att söka efter t.ex. ålder mellan 1985-1988 och sen med bild.
På min kolumnen userPic står sökvägen till bilden om man nu har bild annars om man inte har så är kolumnen tomt.
Får denna felet när jag bara söker efter åldern 1980-1986:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[MySQL][ODBC 3.51 Driver][mysqld-5.0.19-nt]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' and userAge BETWEEN '1980' and '1986' and userLive LIKE '%%' order by us' at line 1
/avancerad2.asp, line 75
Set ObjRS = ObjConn.Execute("SELECT * FROM tblusers WHERE userName LIKE '%"&SafeSQL(Request.Form("userName"))&"%' and userSex LIKE '%"&SafeSQL(Request.Form("userSex"))&"%' and userAge BETWEEN '"&SafeSQL(Request.Form("userAge"))&"' and '"&SafeSQL(Request.Form("userAge2"))&"' and userLive LIKE '%"&SafeSQL(Request.Form("userLive"))&"%'" & " order by userName")
Är UserAge en sträng? eller ett tal? om det är ett tal så ska du inte ha '
mySQL: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between
Samma fel :(
Fortfarande samam fel. Har gjort som det står på MySQL's sida.
Set ObjRS = ObjConn.Execute("SELECT * FROM tblusers WHERE userName LIKE '%"&SafeSQL(Request.Form("userName"))&"%' and userSex LIKE '%"&SafeSQL(Request.Form("userSex"))&"%' and userAge BETWEEN "&SafeSQL(Request.Form("userAge"))&" and "&SafeSQL(Request.Form("userAge2"))&" and userLive LIKE '%"&SafeSQL(Request.Form("userLive"))&"%'" & " order by userName")
vad står det i din SQL om du skriver ut den?
Response.write "SELECT * FROM tblusers WHERE userName LIKE '%"&SafeSQL(Request.Form("userName"))&"%' and userSex LIKE '%"&SafeSQL(Request.Form("userSex"))&"%' and userAge BETWEEN "&SafeSQL(Request.Form("userAge"))&" and "&SafeSQL(Request.Form("userAge2"))&" and userLive LIKE '%"&SafeSQL(Request.Form("userLive"))&"%'" & " order by userName"
Response.End
SELECT * FROM tblusers WHERE userName LIKE '%%' and userSex '' and userAge BETWEEN 1990 and 1986 and userLive LIKE '%%' order by userName
SGuncel skrev:
SELECT * FROM tblusers WHERE userName LIKE '%%' and userSex '' and userAge BETWEEN 1990 and 1986 and userLive LIKE '%%' order by userName
and userSex ''
är ju lite tokigt.
Hur menar du med lite tokigt? :)
emission skrev:
Ja, det måste ju vara
and userSex [b]LIKE '%%' [/b]eller likn.
Får fortfarande samma fel när jag har så.
Och den färdiga SQL-strängen ser nu ut hur?
Den ser ut så hära:
Set ObjRS = ObjConn.Execute("SELECT * FROM tblusers WHERE userName LIKE '%"&SafeSQL(Request.Form("userName"))&"%' and userSex LIKE '%"&SafeSQL(Request.Form("userSex"))&"%' and userAge BETWEEN "&SafeSQL(Request.Form("userAge"))&" and "&SafeSQL(Request.Form("userAge2"))&" and userLive LIKE '%"&SafeSQL(Request.Form("userLive"))&"%'" & " order by userName")
Det är inte din färdiga SQL sträng... det är hur du bygger upp den. emission menar ren text hur din sql "körs".