---
title: "Avancerad Sökning funktion"
type: "forum-thread"
url: "https://www.webforum.nu/amne/databaser-sql/167647-avancerad-sökning-funktion"
topic: "Databaser & SQL"
topic_url: "https://www.webforum.nu/amne/databaser-sql"
author: "SGuncel"
published: "2007-12-29T16:45:15.000Z"
updated: "2008-01-14T19:33:24.000Z"
replies: 27
views: 1239
page: 1
pages: 2
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/databaser-sql/167647-avancerad-sökning-funktion"
---

# Avancerad Sökning funktion

_Sida 1 av 2._

## #1 — SGuncel, 2007-12-29T16:45Z

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")
```

Permalänk: https://www.webforum.nu/p/167647

## #2 — voigtann1, 2007-12-29T17:38Z

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

Fast:

```
username like '%" & request.form("1") & "%' and username like '%....
```

Permalänk: https://www.webforum.nu/p/2066724

## #3 — LarsG, 2007-12-30T15:04Z

Flyttas från ASP

Permalänk: https://www.webforum.nu/p/2066895

## #4 — SGuncel, 2007-12-31T05:45Z

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.

Permalänk: https://www.webforum.nu/p/2067058

## #5 — voigtann1, 2007-12-31T10:39Z

SQL: [Between](http://www.w3schools.com/sql/sql_between.asp)

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

Permalänk: https://www.webforum.nu/p/2067079

## #6 — SGuncel, 2007-12-31T20:03Z

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")
```

Permalänk: https://www.webforum.nu/p/2067157

## #7 — voigtann1, 2007-12-31T20:56Z

Ä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>

Permalänk: https://www.webforum.nu/p/2067161

## #8 — SGuncel, 2007-12-31T22:03Z

Samma fel :(

Permalänk: https://www.webforum.nu/p/2067162

## #9 — voigtann1, 2007-12-31T22:26Z

<http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between>

Permalänk: https://www.webforum.nu/p/2067165

## #10 — SGuncel, 2008-01-01T20:51Z

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")
```

Permalänk: https://www.webforum.nu/p/2067342

## #11 — voigtann1, 2008-01-02T14:30Z

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
```

Permalänk: https://www.webforum.nu/p/2067451

## #12 — SGuncel, 2008-01-02T23:37Z

```
SELECT * FROM tblusers WHERE userName LIKE '%%' and userSex '' and userAge BETWEEN 1990 and 1986 and userLive LIKE '%%' order by userName
```

Permalänk: https://www.webforum.nu/p/2067592

## #13 — emission, 2008-01-03T00:02Z

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

Permalänk: https://www.webforum.nu/p/2067594

## #14 — SGuncel, 2008-01-03T08:13Z

Hur menar du med lite tokigt? :)

Permalänk: https://www.webforum.nu/p/2067608

## #15 — emission, 2008-01-03T08:22Z

Ja, det måste ju vara 

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

eller likn.

Permalänk: https://www.webforum.nu/p/2067613

## #16 — voigtann1, 2008-01-03T08:39Z

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

```
<input type="radio" name="userSex">Visa alla
```

Permalänk: https://www.webforum.nu/p/2067620

## #17 — SGuncel, 2008-01-03T16:06Z

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

Permalänk: https://www.webforum.nu/p/2067721

## #18 — emission, 2008-01-03T20:16Z

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

Permalänk: https://www.webforum.nu/p/2067769

## #19 — SGuncel, 2008-01-04T16:15Z

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")
```

Permalänk: https://www.webforum.nu/p/2067979

## #20 — voigtann1, 2008-01-04T16:35Z

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

Permalänk: https://www.webforum.nu/p/2067980

---

Tråden på webben: https://www.webforum.nu/amne/databaser-sql/167647-avancerad-sökning-funktion  
Nästa sida: https://www.webforum.nu/amne/databaser-sql/167647-avancerad-sökning-funktion/page2.md
