webForumDet fria alternativet

Avancerad Sökning funktion

Databaser & SQLur Databashanterare & SQL

27 svar · 1 201 visningar · startad av SGuncel

Medlem sedan jan. 2007217 inlägg
Frågan#1

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")
Medlem sedan juni 20019 519 inlägg
#2

Prova?
Du kan ha hur många like frågor du vill.

Fast:

username like '%" & request.form("1") & "%' and username like '%....
Medlem sedan dec. 200012 464 inlägg
#3

Flyttas från ASP

Medlem sedan jan. 2007217 inlägg
#4

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&nbsp;
	<input type="radio" name="userSex" value="Flicka"> Flickor&nbsp;
	<input type="radio" name="userSex" value="Kille"> Killar&nbsp;
	<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.

Medlem sedan juni 20019 519 inlägg
#5

SQL: Between

och userSex och andra värden bör ju inte vara "like" då den har fasta värden: Flicka eller Kille

Medlem sedan jan. 2007217 inlägg
#6

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")
Medlem sedan juni 20019 519 inlägg
#7

Ä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

Medlem sedan jan. 2007217 inlägg
#8

Samma fel :(

Medlem sedan juni 20019 519 inlägg
Medlem sedan jan. 2007217 inlägg
#10

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")
Medlem sedan juni 20019 519 inlägg
#11

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
Medlem sedan jan. 2007217 inlägg
#12
SELECT * FROM tblusers WHERE userName LIKE '%%' and userSex '' and userAge BETWEEN 1990 and 1986 and userLive LIKE '%%' order by userName
Medlem sedan dec. 19996 721 inlägg
#13

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.

Medlem sedan jan. 2007217 inlägg
#14

Hur menar du med lite tokigt? :)

Medlem sedan dec. 19996 721 inlägg
#15

Ja, det måste ju vara

and userSex [b]LIKE '%%' [/b]

eller likn.

Medlem sedan juni 20019 519 inlägg
#16

Det är mitt fel nog. Såg inte att du hade:

<input type="radio" name="userSex">Visa alla
Medlem sedan jan. 2007217 inlägg
#17

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å.

Medlem sedan dec. 19996 721 inlägg
#18

Och den färdiga SQL-strängen ser nu ut hur?

Medlem sedan jan. 2007217 inlägg
#19

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")
Medlem sedan juni 20019 519 inlägg
#20

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".

140 ms totalt · 3 externa anrop · v20260731065814-full.4bcf49fe
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
137 ms — hämta tråd, inlägg och bilagor (db)