Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default syntax problem

    hi!!
    This is my problem. Im planing this without success

    *i have 3 tables table1 table2 and table3...
    *The tables have some fields with the same name for example
    both table 2 and table 3 have the field "age"
    *The field id is a key

    select * from a table1, b table2, c table3 where a.id=session("memberId")
    and b.id= session("memberId") and c.id=session("memberId")

    MY QUESTION ARE
    How do i get data from tables that have the same field name like the example
    table 2 and table 3???
    My guess are something like this:

    response.write recordsset(b.age) and for the other
    response.write recordset(c.age)..

    This is wrong syntax i know, but how do i write the syntax in asp?



    Mikael Hellström Guest

  2. Similar Questions and Discussions

    1. <cfquery> syntax problem
      Guys can someone tell me whats wrong with the line I have put in bold? Im trying to run this query but I get an error telling me the syntax is wrong...
    2. INSERT MAX(ID) Syntax problem
      Trying to get the ID (autonumber) of a record that has just been inserted and insert it into a different table but I either get a syntax error or a...
    3. Syntax Coloring Problem with Xemacs
      hello I have Xemacs 21.4 and Ruby 1.8.0 whenever I type $' in my Ruby code Xemacs treats the following Code as string and everything is green....
    4. IE problem with -> syntax
      Hello, My environment is win2k, apache2, php 4 something. My browser is IE. My example is this. $a = new LinkName(); $b =...
    5. Syntax error problem
      The following is the source of a a function I am trying to define in Informix IDS 9.3 (Windows 2000 Server). As far as I can figure, the syntax...
  3. #2

    Default Re: syntax problem

    Replies inline:
    Mikael Hellström wrote:
    >
    > *i have 3 tables table1 table2 and table3...
    > *The tables have some fields with the same name for example
    > both table 2 and table 3 have the field "age"
    > *The field id is a key
    >
    > select * from a table1, b table2, c table3 where
    Why do your table aliases come before the table names here? This looks like
    an invalid query. What is your database?
    > a.id=session("memberId") and b.id= session("memberId") and
    > c.id=session("memberId")
    >
    Unless you are deliberately doing a cross-join (cartesian product), your
    example query here does not seem to be correct. You should be relating the
    tables to each other. Depending on what your database is, you should be
    using the ANSI standard syntax (assuming id is a numeric column):

    " ... from table1 a INNER JOIN table2 b
    ON a.id=b.id INNER JOIN table3 c ON a.id=c.id
    WHERE a.id=" & session("memberId")
    > MY QUESTION ARE
    > How do i get data from tables that have the same field name like the
    > example table 2 and table 3???
    > My guess are something like this:
    >
    > response.write recordsset(b.age) and for the other
    > response.write recordset(c.age)..
    >
    > This is wrong syntax i know, but how do i write the syntax in asp?
    This is yet another reason to avoid using "select *" (SelStar) in production
    code. You should always explicitly list the columns you want your query to
    return. In this case, it will allow you to use column aliases to
    differentiate the like-named columns from each table. Like this (use your
    own nomenclature - "AgeFrom_" is merely for the sake of the example):

    Select a.id, a.col1,b.age As AgeFromB, c.age As AgeFromC ...

    Then, after you open the recordset, you can use the column alias to specify
    the specific age you wish to read:
    response.write recordset("AgeFromB")

    Avoiding SelStar will also help you avoid retrieving unnecessary data. For
    example, why retrieve the id column from tables 2 and 3? You know they are
    going to contain the same data as the id column in table 1: why retrieve
    unnecessary data and cause extra network traffic and unnecessary use of
    resources?


    HTH,
    Bob Barrows

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"


    Bob Barrows Guest

  4. #3

    Default Syntax Problem

    Hi,
    Im trying to get his SQl to work but run thru a query analiser tells me
    there is a syntax error and in the browser returns no results - any ideas?

    SELECT m.Username, m.Author_ID, au.Author_ID AS ActiveUserAuthorID
    FROM tblBuddyList bl INNER JOIN Members m ON bl.Buddy_ID = m.Author_ID
    LEFT JOIN tblActiveUser au ON bl.Buddy_ID = au.Author_ID WHERE bl.Author_ID
    = @LoggedInUserID AND bl.Buddy_ID <> 2
    ORDER BY m.Username ASC;

    It worked before modification from:

    SELECT tblBuddyList.*, Members.Username, Members.Author_ID
    FROM Members INNER JOIN tblBuddyList ON Members.Author_ID
    = tblBuddyList.Buddy_ID WHERE tblBuddyList.Author_ID=" & lngLoggedInUserID &
    "
    AND tblBuddyList.Buddy_ID <> 2
    ORDER BY Members.Username ASC;

    Thanks in advance

    Lee


    Lee Mundie Guest

  5. #4

    Default Re: Syntax Problem

    What is the EXACT error message (there are different kinds of syntax errors;
    VBScript or from the database)? What is the EXACT code you are using? What
    database and version are you using? Are you connecting via ODBC or OLEDB?

    <followups set to asp.db only>

    --
    Aaron Bertrand
    SQL Server MVP
    [url]http://www.aspfaq.com/[/url]




    "Lee Mundie" <lee.j.mundie@ntlworld.com> wrote in message
    news:0GJvb.1965$B03.1220806@newsfep2-win.server.ntli.net...
    > Hi,
    > Im trying to get his SQl to work but run thru a query analiser tells
    me
    > there is a syntax error and in the browser returns no results - any ideas?
    >
    > SELECT m.Username, m.Author_ID, au.Author_ID AS ActiveUserAuthorID
    > FROM tblBuddyList bl INNER JOIN Members m ON bl.Buddy_ID = m.Author_ID
    > LEFT JOIN tblActiveUser au ON bl.Buddy_ID = au.Author_ID WHERE
    bl.Author_ID
    > = @LoggedInUserID AND bl.Buddy_ID <> 2
    > ORDER BY m.Username ASC;
    >
    > It worked before modification from:
    >
    > SELECT tblBuddyList.*, Members.Username, Members.Author_ID
    > FROM Members INNER JOIN tblBuddyList ON Members.Author_ID
    > = tblBuddyList.Buddy_ID WHERE tblBuddyList.Author_ID=" & lngLoggedInUserID
    &
    > "
    > AND tblBuddyList.Buddy_ID <> 2
    > ORDER BY Members.Username ASC;
    >
    > Thanks in advance
    >
    > Lee
    >
    >

    Aaron Bertrand [MVP] Guest

  6. #5

    Default Re: Syntax Problem

    I was going to ask if this was Access or SQL Server, but "@LoggedInUserID"
    leads me to lean more towards SQL Server. Please don't make us guess. :-)
    The version of SQL Server may also be relevant.

    Lee Mundie wrote:
    > Hi,
    > Im trying to get his SQl to work but run thru a query analiser
    > tells me there is a syntax error and in the browser returns no
    > results - any ideas?
    >
    > SELECT
    m.Username,
    m.Author_ID,
    au.Author_ID AS ActiveUserAuthorID
    > FROM tblBuddyList bl
    INNER JOIN Members m ON bl.Buddy_ID = m.Author_ID
    > LEFT JOIN tblActiveUser au ON bl.Buddy_ID = au.Author_ID
    WHERE
    > bl.Author_ID = @LoggedInUserID
    AND bl.Buddy_ID <> 2
    > ORDER BY m.Username ASC;
    Hmm, so I'm assuming this is from a stored procedure? When you try to run it
    in Query Analyzer, do you declare the @LoggedInUserID variable and give it a
    value?
    >
    > It worked before modification from:
    >
    > SELECT tblBuddyList.*, Members.Username, Members.Author_ID
    > FROM Members INNER JOIN tblBuddyList ON Members.Author_ID
    > = tblBuddyList.Buddy_ID WHERE tblBuddyList.Author_ID=" &
    > lngLoggedInUserID & "
    > AND tblBuddyList.Buddy_ID <> 2
    > ORDER BY Members.Username ASC;
    So I'm correctly assuming you're rewriting this to get away from dynamic
    sql? Good!

    Bob Barrows

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"


    Bob Barrows Guest

  7. #6

    Default Re: Syntax Problem

    Hi,
    It appears to be a VBScript Error syntax, at the INNER JOIN - using ASP,
    Access DB via ODBC on JET4. The final intention is to SQL the DB, but find
    it easier in initial stages to dev on Access.

    Still kinda getting there with ASP, so forgive if vague or stoopid
    somethimes!!!

    This is the exact code:

    SELECT m.Username, m.Author_ID, au.Author_ID AS ActiveUserAuthorID
    FROM tblBuddyList bl INNER JOIN Members m ON bl.Buddy_ID = m.Author_ID
    LEFT JOIN tblActiveUser au ON bl.Buddy_ID = au.Author_ID WHERE bl.Author_ID
    = @LoggedInUserI AND bl.Buddy_ID <> 2
    ORDER BY m.Username ASC;

    Thanks in Advance

    Lee


    Lee Mundie Guest

  8. #7

    Default Re: Syntax Problem

    Lee Mundie wrote:
    > Hi,
    > It appears to be a VBScript Error syntax, at the INNER JOIN - using
    > ASP, Access DB via ODBC on JET4. The final intention is to SQL the
    > DB, but find it easier in initial stages to dev on Access.
    Very good idea. Even better if you save the query in Access and run the
    saved query from ASP. I've posted many examples of vbscript code to run
    saved parameter queries in Access - a quick Google seach should easily turn
    them up.
    >
    > Still kinda getting there with ASP, so forgive if vague or stoopid
    > somethimes!!!
    >
    > This is the exact code:
    >
    > SELECT m.Username, m.Author_ID, au.Author_ID AS ActiveUserAuthorID
    > FROM tblBuddyList bl INNER JOIN Members m ON bl.Buddy_ID = m.Author_ID
    > LEFT JOIN tblActiveUser au ON bl.Buddy_ID = au.Author_ID WHERE
    > bl.Author_ID = @LoggedInUserI AND bl.Buddy_ID <> 2
    > ORDER BY m.Username ASC;
    >
    > Thanks in Advance
    >
    > Lee
    Excuse me, but how can Author_ID, presumably a Number field, ever be equal
    to @LoggedInUserI, a non-delimited string?

    Bob Barrows

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"


    Bob Barrows Guest

  9. #8

    Default Re: Syntax Problem

    Lee Mundie wrote:
    > Hi,
    > It appears to be a VBScript Error syntax, at the INNER JOIN - using
    > ASP, Access DB via ODBC on JET4. The final intention is to SQL the
    > DB, but find it easier in initial stages to dev on Access.
    >
    > Still kinda getting there with ASP, so forgive if vague or stoopid
    > somethimes!!!
    >
    > This is the exact code:
    >
    > SELECT m.Username, m.Author_ID, au.Author_ID AS ActiveUserAuthorID
    > FROM tblBuddyList bl INNER JOIN Members m ON bl.Buddy_ID = m.Author_ID
    > LEFT JOIN tblActiveUser au ON bl.Buddy_ID = au.Author_ID WHERE
    > bl.Author_ID = @LoggedInUserI AND bl.Buddy_ID <> 2
    > ORDER BY m.Username ASC;
    >
    OK, you're trying to use @LoggedInUserI as a parameter right? You need to
    surround it with brackets []

    Also, Access is very "fussy" about joining more than two tables: it wants
    you to group the joins using parentheses. I suggest using GUI Design View to
    create your basic query, putting all the tables in and creating all the
    joins. Then switch to SQL View to see where Access puts the grouping
    parentheses in the FROM clause.

    HTH,
    Bob Barrows

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"


    Bob Barrows Guest

  10. #9

    Default Re: Syntax Problem

    > It appears to be a VBScript Error syntax,

    "Appears to be"? Could you COPY & PASTE the error message from the browser?
    > This is the exact code:
    >
    > SELECT m.Username, m.Author_ID, au.Author_ID AS ActiveUserAuthorID
    > FROM tblBuddyList bl INNER JOIN Members m ON bl.Buddy_ID = m.Author_ID
    > LEFT JOIN tblActiveUser au ON bl.Buddy_ID = au.Author_ID WHERE
    bl.Author_ID
    > = @LoggedInUserI AND bl.Buddy_ID <> 2
    > ORDER BY m.Username ASC;
    No, that is not ASP code, that is plain text. What I mean by code is the
    ASP code that generates the error.

    --
    Aaron Bertrand
    SQL Server MVP
    [url]http://www.aspfaq.com/[/url]


    Aaron Bertrand [MVP] Guest

  11. #10

    Default Re: Syntax Problem

    Lee,

    what value do u set to @LoggedInUserID?

    "Lee Mundie" <lee.j.mundie@ntlworld.com> wrote in message
    news:0GJvb.1965$B03.1220806@newsfep2-win.server.ntli.net...
    > Hi,
    > Im trying to get his SQl to work but run thru a query analiser tells
    me
    > there is a syntax error and in the browser returns no results - any ideas?
    >
    > SELECT m.Username, m.Author_ID, au.Author_ID AS ActiveUserAuthorID
    > FROM tblBuddyList bl INNER JOIN Members m ON bl.Buddy_ID = m.Author_ID
    > LEFT JOIN tblActiveUser au ON bl.Buddy_ID = au.Author_ID WHERE
    bl.Author_ID
    > = @LoggedInUserID AND bl.Buddy_ID <> 2
    > ORDER BY m.Username ASC;
    >
    > It worked before modification from:
    >
    > SELECT tblBuddyList.*, Members.Username, Members.Author_ID
    > FROM Members INNER JOIN tblBuddyList ON Members.Author_ID
    > = tblBuddyList.Buddy_ID WHERE tblBuddyList.Author_ID=" & lngLoggedInUserID
    &
    > "
    > AND tblBuddyList.Buddy_ID <> 2
    > ORDER BY Members.Username ASC;
    >
    > Thanks in advance
    >
    > Lee
    >
    >

    Guest

  12. #11

    Default Syntax problem

    Okay, I'm a newbie, the following script are commented and explained, but
    the something is wrong with the syntax. Anyone, any idea?

    <php for login check and redirect of users according to their login details
    against data in MySQL>

    // connection - custnumber and orgnmmer associated with similar login fields
    in the login form, values must be equal to DB-fields
    mysql_connect("my.ip.address", "$custnumber", "@orgnummer") or
    die(mysql_error());
    mysql_select_db("custdatabase") or die(mysql_error());

    // select relevant table info
    $query "SELECT custnumber,orgnummer,rabkat FROM kunder";

    //check to see if login details are equal to those in db and whether
    "rabkat" are value 10 or 20, redirect users logged in according to the value
    in DB under column "rabkat"

    if (custnumber=="$custnumber")
    {
    orgunmmer=="$orgnummer";
    rabkat==10;
    echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5; URL=html-redirect1.html\">";
    }

    if (custnumber=="$custnumber")
    {
    orgnummer=="$orgnummer";
    rabkat==20;
    echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5; URL=html-redirect2.html\">";
    }
    else if (custnumber=="$custnumber")
    {
    orgnummer=="$orgnummer";
    rabkat="";
    echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5; URL=html-redirect3.html\">";
    }


    John Olav Guest

  13. #12

    Default Re: Syntax problem

    John Olav wrote:
    > Okay, I'm a newbie, the following script are commented and explained, but
    > the something is wrong with the syntax. Anyone, any idea?
    >
    > <php for login check and redirect of users according to their login
    > details against data in MySQL>
    >
    > // connection - custnumber and orgnmmer associated with similar login
    > fields in the login form, values must be equal to DB-fields
    > mysql_connect("my.ip.address", "$custnumber", "@orgnummer") or
    > die(mysql_error());
    > mysql_select_db("custdatabase") or die(mysql_error());
    >
    > // select relevant table info
    > $query "SELECT custnumber,orgnummer,rabkat FROM kunder";
    >
    > //check to see if login details are equal to those in db and whether
    > "rabkat" are value 10 or 20, redirect users logged in according to the
    > value in DB under column "rabkat"
    >
    > if (custnumber=="$custnumber")
    > {
    > orgunmmer=="$orgnummer";
    > rabkat==10;
    > echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5;
    > URL=html-redirect1.html\">"; }
    >
    > if (custnumber=="$custnumber")
    > {
    > orgnummer=="$orgnummer";
    > rabkat==20;
    > echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5;
    > URL=html-redirect2.html\">"; }
    > else if (custnumber=="$custnumber")
    > {
    > orgnummer=="$orgnummer";
    > rabkat="";
    > echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5;
    > URL=html-redirect3.html\">"; }

    I can't commment on the php, having never touched it, but was is obvious to
    me is that you have 3 different spellings of org...
    (orgnmmer,orgnummer,orgunmmer). Surely at least 2 of them are wrong.


    --
    Brian Wakem
    Email: [url]http://homepage.ntlworld.com/b.wakem/myemail.png[/url]
    Brian Wakem Guest

  14. #13

    Default Re: Syntax problem

    Hi, John,

    Comments interspaced below...

    John Olav wrote:
    > Okay, I'm a newbie, the following script are commented and explained, but
    > the something is wrong with the syntax. Anyone, any idea?
    >
    > <php for login check and redirect of users according to their login details
    > against data in MySQL>
    >
    > // connection - custnumber and orgnmmer associated with similar login fields
    > in the login form, values must be equal to DB-fields
    > mysql_connect("my.ip.address", "$custnumber", "@orgnummer") or
    > die(mysql_error());
    > mysql_select_db("custdatabase") or die(mysql_error());
    >
    > // select relevant table info
    > $query "SELECT custnumber,orgnummer,rabkat FROM kunder";
    >
    This is invalid syntax. You need to call mysql_query to actually do the query, i.e.

    $query = "SELECT custnumber,orgnummer,rabkat FROM kunder";
    $result= mysql_query($query);
    > //check to see if login details are equal to those in db and whether
    > "rabkat" are value 10 or 20, redirect users logged in according to the value
    > in DB under column "rabkat"
    >
    You've done the query, but now you must fetch the result:

    $data = mysql_fetch_array($result);

    Results of the first matching row will be in $data['custnumber'],
    $data['orgnummer'], etc.
    > if (custnumber=="$custnumber")
    if ($data['custnumber'] == $custnumber)

    I'm assuming $custnumber is a variable here - so no quotes around it
    > {
    > orgunmmer=="$orgnummer";
    $orgnummer = $data['orgnummer'];
    > rabkat==10;
    > echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5; URL=html-redirect1.html\">";
    > }
    >
    > if (custnumber=="$custnumber")
    Not sure why you're checking this again - it's the same data as before
    > {
    > orgnummer=="$orgnummer";
    > rabkat==20;
    > echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5; URL=html-redirect2.html\">";
    > }
    > else if (custnumber=="$custnumber")
    > {
    > orgnummer=="$orgnummer";
    > rabkat="";
    > echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5; URL=html-redirect3.html\">";
    > }
    >
    >
    If you're looking for a specific row, you should use the WHERE clause to fetch
    that row, i.e.

    $query = "SELECT orgnummer,rabkat FROM kunder WHERE custnumber=$custnumber";

    Assuming that $custnumber is a numeric value.

    Hopefully this will get you started. I'm not sure what you're trying to do, so
    I can't comment further on your code structure.

    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    [email]jstucklex@attglobal.net[/email]
    ==================
    Jerry Stuckle Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139