Avatar billede benkebus Nybegynder
11. november 2003 - 09:12 Der er 30 kommentarer

Lägga till en count i en sql

SELECT kalla,forfattare,rubrik,text,datum,kategori,nID from nyheter WHERE (kalla LIKE '%lorem%') OR (forfattare LIKE '%lorem%') OR (rubrik LIKE '%lorem%') OR (text LIKE '%lorem%') OR (datum LIKE '%lorem%') ORDER BY datum;

How do you count the number of records that above sqlstatement produces?
Avatar billede medions Nybegynder
11. november 2003 - 09:19 #1
SELECT COUNT(nID) As Antal, kalla,forfattare,rubrik,text,datum,kategori,nID from nyheter WHERE (kalla LIKE '%lorem%') OR (forfattare LIKE '%lorem%') OR (rubrik LIKE '%lorem%') OR (text LIKE '%lorem%') OR (datum LIKE '%lorem%') ORDER BY datum;

Og udskriv objRs("Antal")

//>Rune
Avatar billede benkebus Nybegynder
11. november 2003 - 09:22 #2
Error Type:
Microsoft JET Database Engine (0x80040E21)
Du försökte köra en fråga med en mängdfunktion som inte innehöll det angivna uttrycket 'kalla'.
Avatar billede medions Nybegynder
11. november 2003 - 09:23 #3
SELECT COUNT(nID) kalla,forfattare,rubrik,text,datum,kategori,nID from nyheter WHERE (kalla LIKE '%lorem%') OR (forfattare LIKE '%lorem%') OR (rubrik LIKE '%lorem%') OR (text LIKE '%lorem%') OR (datum LIKE '%lorem%') ORDER BY datum;

Og udskriv objRs(0)

//>Rune
Avatar billede medions Nybegynder
11. november 2003 - 09:24 #4
Ellers prøv:
SELECT COUNT(*) kalla,forfattare,rubrik,text,datum,kategori,nID from nyheter WHERE (kalla LIKE '%lorem%') OR (forfattare LIKE '%lorem%') OR (rubrik LIKE '%lorem%') OR (text LIKE '%lorem%') OR (datum LIKE '%lorem%') ORDER BY datum;

Og udskriv objRs(0)
Avatar billede benkebus Nybegynder
11. november 2003 - 09:34 #5
SELECT COUNT(*) kalla,forfattare,rubrik,text,datum,kategori from nyheter WHERE...
resultatet:
Error Type:
Microsoft JET Database Engine (0x80040E14)
Syntaxfel (operator saknas) i frågeuttrycket 'COUNT(*) kalla'.
/axelsons/searchnyheter.asp, line 202

line202:
<%Set rs = db.Execute(strSQL)%>
som hör till:
<%
Set db = Server.CreateObject("ADODB.Connection")
db.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
        &"Data Source=C:\documents and settings\benke\skrivbord\webarbeten\axelsons\db\axelsons.mdb;"
%>
Avatar billede medions Nybegynder
11. november 2003 - 09:49 #6
Hov...

SELECT COUNT(*), kalla,forfattare,rubrik,text,datum,kategori,nID from nyheter WHERE (kalla LIKE '%lorem%') OR (forfattare LIKE '%lorem%') OR (rubrik LIKE '%lorem%') OR (text LIKE '%lorem%') OR (datum LIKE '%lorem%') ORDER BY datum;

Og udskriv rs(0)

//>Rune
Avatar billede medions Nybegynder
11. november 2003 - 09:51 #7
Men det skal nok højst sandsynlig være sådan her eftersom du oxo har et reseveret ord i din SQL (Text)

SELECT COUNT(*), kalla,forfattare,rubrik,[text],datum,kategori,nID from nyheter WHERE (kalla LIKE '%lorem%') OR (forfattare LIKE '%lorem%') OR (rubrik LIKE '%lorem%') OR (text LIKE '%lorem%') OR (datum LIKE '%lorem%') ORDER BY datum;

Og udskriv rs(0)

//>Rune
Avatar billede benkebus Nybegynder
11. november 2003 - 09:51 #8
Error Type:
Microsoft JET Database Engine (0x80040E21)
Du försökte köra en fråga med en mängdfunktion som inte innehöll det angivna uttrycket 'kalla'.
/axelsons/searchnyheter.asp, line 202
Avatar billede medions Nybegynder
11. november 2003 - 09:52 #9
Prøv at poste hele din kode her...

//>Rune
Avatar billede benkebus Nybegynder
11. november 2003 - 09:54 #10
<%

       
    strSearchCriteria = request.form("searcher")
    strTemp = "SELECT COUNT(*), [kalla],forfattare,rubrik,[text],datum,kategori from nyheter WHERE "
           
    ReDim arrFieldsToSearch(5)           
    arrFieldsToSearch(0) = "kalla"
    arrFieldsToSearch(1) = "forfattare"
    arrFieldsToSearch(2) = "rubrik"
    arrFieldsToSearch(3) = "text"
    arrFieldsToSearch(4) = "datum"
    arrFieldsToSearch(5) = "kategori"
           
    'get the sql string   
    strSQL = SQLSearchWordsTableAndJoin(strTemp, arrFieldsToSearch, "datum", strSearchCriteria, False, NULL)
    'strSQL = SQLSearchWordsTableAndJoin(str
    '    Temp, arrFieldsToSearch, "tblCD.title",
    '    strSearchCriteria, False, "tblCD.display
    '    = true")
    ' ######################################
    '    ########################################
    '    ######################################
    ' #
    ' # Procedure:SQLSearchWordsTableAndJoin
    '    (strTableName, arrFieldsToSearch, strOrd
    '    erByField, strSearchCriteria, bSearchMus
    '    tMatchAllWords, strExtraCrit)
    ' # Purpose:     Search database for keywo
    '    rds on text fields.
    ' # Date:        29-11-2000
    ' # inputs: strTableAndJoin            - as strin
    '    g. table and join string
    ' #                arrFieldsToSearch            - as array.
    '    The field names to search on.
    ' #                strOrderByField            - as string. T
    '    he field to order the query on.
    ' #                strSearchCriteria            - as string.
    '    The form value - search criteria.
    ' #                bSearchMustMatchAllWords    - as bo
    '    olean. Search must match all words or no
    '    t.
    ' #                strExtraCrit                - as string. Ext
    '    ra criteria that the user has specified
    ' # outputs: sql string.
    ' #                This code is modified from a 4gu
    '    ysfromrolla function
    ' #
    ' ######################################
    '    ########################################
    '    ######################################
    function SQLSearchWordsTableAndJoin(strTableAndJoin, arrFieldsToSearch, strOrderByField, strSearchCriteria, bSearchMustMatchAllWords, strExtraCrit)
        Dim strSQL                'as string.
        Dim arrWords()            'as array. Holds all of the words
        Dim intWordCounter        'as integer. Counts the number of words To search on.
        Dim I                    'as integer. Standard looping variable
        Dim intCurrentWord        'as integer. The current word.
        Dim strTemp                'as string. A temp variable that is used To build the sql string.
        Dim intArrayCounter        'as integer. Standard looping variable
        Dim intElementsInArray    'as integer. Gets the number of elements in the array. Fields To search on.
       
        'get the number of elements In the array
        intElementsInArray = CInt(UBound(arrFieldsToSearch) - 1)
       
        'prepare String
        strSearchCriteria = Trim(strSearchCriteria)
        strSearchCriteria = Replace(strSearchCriteria, " "," ")
        strSearchCriteria = Replace(strSearchCriteria, ""," ")
        strSearchCriteria = Replace(strSearchCriteria, ""," ")
       
        'count spaces because Each space would mean a new word
        For I = 1 To Len(strSearchCriteria)
            if mid(strSearchCriteria, I, 1) = " " Then intWordCounter = intWordCounter + 1
        Next
        ' Now, We Add One To Include The First Word
        intWordCounter = intWordCounter + 1
        ReDim arrWords(intWordCounter)
       
        ' Now, We Fill The Array With The Words
        intCurrentWord = 1
        For I = 1 To len(strSearchCriteria)
            if mid(strSearchCriteria, i, 1) = " " Then
                intCurrentWord = intCurrentWord + 1
            Else
                arrWords(intCurrentWord) = arrWords(intCurrentWord) + mid(strSearchCriteria, I, 1)
            End if
        Next
        ' Now Lets Build The SQL Statement Based On What Search Type bSearchMustMatchAllWords
        ' First Part Of SQL
        strSQL = strTableAndJoin & " "
        'loop through array To Get all of the fields
        For intArrayCounter = 0 To CInt(intElementsInArray)
       
            ' For Loop To Concatenate SQL String Together
            For I = 1 To intWordCounter
                if CBool(bSearchMustMatchAllWords) Then
                    if CInt(I) = 1 Then strTemp = strTemp & "(" 'only put ( first time into Loop
                    if I <> intWordCounter Then
                        strTemp = strTemp & arrFieldsToSearch(intArrayCounter) & " LIKE '%" & arrWords(I) & "%' AND "
                    ElseIf I = intWordCounter Then
                        strTemp = strTemp & arrFieldsToSearch(intArrayCounter) & " LIKE '%" & arrWords(I) & "%'"
                    End if
                    if (strExtraCrit <> "" And CInt(i) = CInt(intWordCounter)) Then strTemp = strTemp & " " & strExtraCrit & " "
                   
                    if CInt(i) = intWordCounter Then strTemp = strTemp & ")"'only put ) last time into Loop
                Else
                    if I <> intWordCounter Then
                        strTemp = strTemp & "(" & arrFieldsToSearch(intArrayCounter) & " LIKE '%" & arrWords(I) & "%'"
                        if strExtraCrit <> "" Then strTemp = strTemp & " " & strExtraCrit & " "
                        strTemp = strTemp & ") OR "
                    ElseIf I = intWordCounter Then
                        strTemp = strTemp & "(" & arrFieldsToSearch(intArrayCounter) & " LIKE '%" & arrWords(I) & "%'"
                        if strExtraCrit <> "" Then strTemp = strTemp & " " & strExtraCrit & " "
                        strTemp = strTemp & ")"
                    End if
                End if
            Next 'I
            if CInt(intArrayCounter) < CInt(intElementsInArray) Then strTemp = strTemp & " OR "
        Next 'intArrayCounter
           
       
        'If user specified a order by field add this code To the End of the sql string.
        if Len(strOrderByField) > 0 Or strOrderByField <> "" Then strSQL = strSQL & strTemp & " ORDER BY " & strOrderByField & ";"
        SQLSearchWordsTableAndJoin = strSQL
    End function
    'response.write strSQL
    %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>

<%
Set db = Server.CreateObject("ADODB.Connection")
db.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
        &"Data Source=C:\documents and settings\benke\skrivbord\webarbeten\axelsons\db\axelsons.mdb;"
    'db.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
        '&"Data Source=d:\web\kunder\axelsons\nyhetsdemo\db\axelsons.mdb;"
    Function CutString (str, intCut)
    if len(str) > intCut then
      posstart = InStrRev(str, " ", intCut)
      if (posstart <> 0) then
    CutString = Left(str, posstart) & "...<br><a href=""visanyhet.asp?nID="&rs("nID")&""" class=""lasmer"" target=""sida"">Läs hela artikeln >></a>"
    Exit Function
      end if
    end if
    CutString = str
    End function
%>

    <title>Kurser</title>
<style>
BODY {    BACKGROUND-COLOR: #FFFFFF;FONT-FAMILY: verdana,arial, sans-serif; SCROLLBAR-ARROW-COLOR: #17355d; SCROLLBAR-SHADOW-COLOR: #17355d; SCROLLBAR-FACE-COLOR: #8dd2cb; FONT-SIZE: 10px; SCROLLBAR-HIGHLIGHT-COLOR: #8dd2cb;  COLOR: #17355d; SCROLLBAR-3DLIGHT-COLOR: #17355d;  SCROLLBAR-TRACK-COLOR: #8dd2cb;  SCROLLBAR-DARKSHADOW-COLOR: #8dd2cb }

.rubrik {font-family:verdana, arial, sans-serif; font-size:11px; text-align:left;font-weight:bold;}
.text {font-family:Verdana, arial, sans-serif; font-size:11px; text-align:left; font-style:normal; font-weight:normal;}
.kat {font-family:Verdana, arial, sans-serif; font-size:11px; font-weight:normal; font-style:normal; color:#5d5d5d; text-decoration:none }
TD EM {font-family:Verdana, arial,  sans-serif; font-size:11pt; font-weight:bold; font-style:normal; color:#000000;}
TD EM B {font-family:Verdana, arial, sans-serif; font-size:10pt; font-weight:bold; font-style:normal; color:#ffffff }
a:link    {font-family:Verdana, arial, sans-serif; font-size:11px; font-weight:bold; font-style:normal; color:#000000; text-decoration:none }
a:visited {font-family:Verdana, arial, sans-serif; font-size:11px; font-weight:bold; font-style:normal; color:#000000; text-decoration:none }
a:hover  {font-family:Verdana, arial, sans-serif; font-size:11px; font-weight:bold; font-style:normal; color:#8dd2cb; text-decoration:none }
a.lasmer:link    {font-family:Verdana, arial, sans-serif; font-size:10px; font-weight:normal; font-style:normal; color:#5d5d5d; text-decoration:none }
a.lasmer:visited {font-family:Verdana, arial, sans-serif; font-size:10px; font-weight:normal; font-style:normal; color:#5d5d5d; text-decoration:none }
a.lasmer:hover  {font-family:Verdana, arial, sans-serif; font-size:10px; font-weight:normal; font-style:normal; color:#8dd2cb; text-decoration:underline }
#news {position:absolute; top:15; left:40;}
#bild {position:absolute; top:40; left:330;}
#kursmeny  {position:absolute; top:15; left:530;}
</style>   

    <script type="text/javascript" language="JavaScript">
// Define the location of count.asp
// Using a path, you may use this code in any subfolder
var file='/statistik/count.asp';

var d=new Date();
var s=d.getSeconds();
var m=d.getMinutes();
var x=s*m;
f='' + escape(document.referrer);
if (navigator.appName=='Netscape'){b='NS';}
if (navigator.appName=='Microsoft Internet Explorer'){b='MSIE';}
if (navigator.appVersion.indexOf('MSIE 3')>0) {b='MSIE';}
u='' + escape(document.URL); w=screen.width; h=screen.height;
v=navigator.appName;
fs = window.screen.fontSmoothingEnabled;
if (v != 'Netscape') {c=screen.colorDepth;}
else {c=screen.pixelDepth;}
j=navigator.javaEnabled();
info='w=' + w + '&h=' + h + '&c=' + c + '&r=' + f + '&u='+ u + '&fs=' + fs + '&b=' + b + '&x=' + x;
document.write('<img src="' + file + '?'+info+ '" width=1 height=1 border=0>');

    </script>

</head>

<body background="bilder/bakgrund_ny.gif" marginwidth="0" marginheight="0" topmargin="0" leftmargin="0">

<div id="news">
<%Set rs = db.Execute(strSQL)%>
<table width="451" border="0" cellpadding="0" cellspacing="0">
<tr><td>Sökningen på <%=request.form("searcher")%> resulterade i <%=rs(0)%></td></tr>
<% Do While Not rs.EOF %>
<tr>
<td colspan="5">
<%
response.write "<span class=""rubrik"">" & rs("rubrik") & "</span><br>"
response.write "<span class=""text"">" & CutString(rs("text"),100) & "</span><br><br>"
%>
</td>
</tr>
<%
rs.MoveNext
    Loop%>
</table>
</div>


<div id="kursmeny">
<table border="0" cellpadding="0" cellspacing="0"><tr>
<td width="190"><!--#INCLUDE FILE="nyhetskatmeny.asp" -->
</td></tr>
</table>
</div>
<%
db.Close
Set db = Nothing
%>
</body>
</html>
Avatar billede medions Nybegynder
11. november 2003 - 10:18 #11
Det her skulle altså meget gerne virke:

    strTemp = "SELECT COUNT(*), [kalla],[forfattare],[rubrik],[text],[datum],[kategori] from nyheter WHERE "

Kan du evt. prøve at oversætte din fejlmedd. til engelsk / dansk?

//>Rune
Avatar billede benkebus Nybegynder
11. november 2003 - 10:23 #12
Error Type:
Microsoft JET Database Engine (0x80040E21)
You tried to execute a query that does not include the specified
expression 'status' as part of an aggregate function.
/axelsons/searchnyheter.asp, line 202
Avatar billede benkebus Nybegynder
11. november 2003 - 10:24 #13
and no... your last suggestion didn't work...

and it's supposed to be 'kalla' in the translated errormessage above...
Avatar billede medions Nybegynder
11. november 2003 - 10:24 #14
Ahh, så prøv sådan her:

strTemp = "SELECT COUNT(*), [status], [kalla],[forfattare],[rubrik],[text],[datum],[kategori] from nyheter WHERE "

//>Rune
Avatar billede medions Nybegynder
11. november 2003 - 10:25 #15
Nej, vent, prøv at gøre sådan her:

strTemp = "SELECT COUNT(*), * from nyheter WHERE "

//>Rune
Avatar billede benkebus Nybegynder
11. november 2003 - 10:27 #16
Error Type:
Microsoft JET Database Engine (0x80040E14)
Syntaxfel. i frågeuttrycket 'SELECT COUNT(*)'.
/axelsons/searchnyheter.asp, line 203
Avatar billede medions Nybegynder
11. november 2003 - 10:38 #17
Jamen så prøver vi lgie med:

strTemp = "SELECT COUNT(nID), * from nyheter WHERE "

//>Rune
Avatar billede benkebus Nybegynder
11. november 2003 - 10:43 #18
Error Type:
Microsoft JET Database Engine (0x80040E21)
Det går inte att gruppera fält tillagda med '*'.
/axelsons/searchnyheter.asp, line 203
Avatar billede medions Nybegynder
11. november 2003 - 10:47 #19
Hmm det forstår jeg sq ikke... Prøv sådan her så:

strTemp = "SELECT COUNT(nID).Nyheter, *.Nyheter from nyheter WHERE "

//>Rune
Avatar billede benkebus Nybegynder
11. november 2003 - 10:50 #20
Error Type:
Microsoft JET Database Engine (0x80040E14)
Syntaxfel (operator saknas) i frågeuttrycket '*.Nyheter'.
/axelsons/searchnyheter.asp, line 169

line 169: <%Set rs = db.Execute(strSQL)%>
Avatar billede medions Nybegynder
11. november 2003 - 10:53 #21
Agh
strTemp = "SELECT Nyheter.COUNT(nID) AS Antal, Nyheter.* from nyheter WHERE "

og udskriv det sådan her:
Rs("Antal")

//>Rune
Avatar billede medions Nybegynder
11. november 2003 - 10:54 #22
Jamen for fanden da *G* sådan her:

strTemp = "SELECT COUNT(Nyheter.nID) AS Antal, Nyheter.* from nyheter WHERE "

og udskriv det sådan her:
Rs("Antal")

//>Rune
Avatar billede benkebus Nybegynder
11. november 2003 - 10:56 #23
Error Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][Drivrutin för ODBC Microsoft Access] Det går inte att gruppera fält tillagda med '*' (Nyheter).
/axelsons/searchnyheter.asp, line 108

feels like i'm going insane...
Avatar billede medions Nybegynder
11. november 2003 - 11:52 #24
Hvad hvis du gør sådan her:

strTemp = "SELECT * from nyheter WHERE "

Hvad siger den så?
-og husk lgie at oversætte dine fejlmedd. til engelsk,ellers forstår ejg dem ikke :(

//>Rune
Avatar billede benkebus Nybegynder
11. november 2003 - 11:55 #25
du funkar det som det ska...
no problems...
Avatar billede benkebus Nybegynder
11. november 2003 - 11:56 #26
det funkar menar jag
Avatar billede medions Nybegynder
11. november 2003 - 12:38 #27
Så prøv lige sådan her:

    strTemp = "SELECT COUNT(*) From nyheter WHERE "

//>Rune
Avatar billede benkebus Nybegynder
11. november 2003 - 12:50 #28
Error Type:
Microsoft JET Database Engine (0x80040E21)
You tried to execute a query that does not include the specified
expression 'datum' as part of an aggregate function.
/axelsons/searchnyheter.asp, line 169
Avatar billede medions Nybegynder
11. november 2003 - 12:54 #29
Så burde dette dæleme oxo virke:
strTemp = "SELECT *, COUNT(*) As Antal FROM nyheter WHERE "

//>Rune
Avatar billede benkebus Nybegynder
11. november 2003 - 12:58 #30
Error Type:
Microsoft JET Database Engine (0x80040E21)
Can't group field added with '*'.
/axelsons/searchnyheter.asp, line 169
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