SELECT: Syntax error. Please help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sam

    SELECT: Syntax error. Please help

    When I run this SQL query:

    SELECT u.*, o.*
    FROM users u, orders o
    WHERE
    TO_DAYS(o.order _date)
    BETWEEN
    TO_DAYS('2003-09-20')-10
    AND
    TO_DAYS('2003-09-20')+10
    AND
    o.client_id = u.user_id;

    I get this error:

    You have an arror in your SQL syntax '' at line 1

    As you see, this error message have nothing to do
    with the stucture of my tables or with what they contain.
    SYNTAX ERROR ??????????????? ????

    Help!


  • Jørn-Inge Kristiansen

    #2
    Re: SELECT: Syntax error. Please help

    "syntax error" means that what you wrote is not within the syntax that mysql
    use, occures very often when you
    write something wrong or just have the wrong order in stuff and so on

    in this case I think it's because u referes to the alias u and o of the
    table before u alias them

    it should just be select * from ....



    "sam" <rbaba99@carama il.com> wrote in message
    news:bl91mf$hfq $1@news.cyberci ty.dk...[color=blue]
    > When I run this SQL query:
    >
    > SELECT u.*, o.*
    > FROM users u, orders o
    > WHERE
    > TO_DAYS(o.order _date)
    > BETWEEN
    > TO_DAYS('2003-09-20')-10
    > AND
    > TO_DAYS('2003-09-20')+10
    > AND
    > o.client_id = u.user_id;
    >
    > I get this error:
    >
    > You have an arror in your SQL syntax '' at line 1
    >
    > As you see, this error message have nothing to do
    > with the stucture of my tables or with what they contain.
    > SYNTAX ERROR ??????????????? ????
    >
    > Help!
    >
    >[/color]


    Comment

    • Pedro

      #3
      Re: SELECT: Syntax error. Please help

      [ not posted to alt.php.sql ]

      sam wrote:[color=blue]
      > When I run this SQL query:
      >
      > SELECT u.*, o.*
      > FROM users u, orders o
      > WHERE
      > TO_DAYS(o.order _date)
      > BETWEEN
      > TO_DAYS('2003-09-20')-10
      > AND
      > TO_DAYS('2003-09-20')+10
      > AND
      > o.client_id = u.user_id;
      >
      > I get this error:
      >
      > You have an arror in your SQL syntax '' at line 1
      >
      > As you see, this error message have nothing to do
      > with the stucture of my tables or with what they contain.
      > SYNTAX ERROR ??????????????? ????
      >
      > Help![/color]

      Are you including the semicolon in the string?
      Don't!

      <?php
      $errsql = 'select * from table;';
      $goodsql = 'select * from table';
      ?>

      --
      I have a spam filter working.
      To mail me include "urkxvq" (with or without the quotes)
      in the subject line, or your mail will be ruthlessly discarded.

      Comment

      • Tom Thackrey

        #4
        Re: SELECT: Syntax error. Please help


        On 29-Sep-2003, "sam" <rbaba99@carama il.com> wrote:
        [color=blue]
        > When I run this SQL query:
        >
        > SELECT u.*, o.*
        > FROM users u, orders o
        > WHERE
        > TO_DAYS(o.order _date)
        > BETWEEN
        > TO_DAYS('2003-09-20')-10
        > AND
        > TO_DAYS('2003-09-20')+10
        > AND
        > o.client_id = u.user_id;
        >
        > I get this error:
        >
        > You have an arror in your SQL syntax '' at line 1
        >
        > As you see, this error message have nothing to do
        > with the stucture of my tables or with what they contain.
        > SYNTAX ERROR ??????????????? ????
        >
        > Help![/color]

        I ran the sql string (including the ;) as you wrote it and it does not get a
        syntax error. This suggests that you aren't getting the sql string to MySQL
        intact. Please post the code that defines the sql string and does the query.


        --
        Tom Thackrey

        Comment

        • Pedro

          #5
          Re: SELECT: Syntax error. Please help

          Tom Thackrey wrote:
          [...][color=blue]
          >I ran the sql string (including the ;) as you wrote it and it does not get a
          >syntax error. This suggests that you aren't getting the sql string to MySQL
          >intact. Please post the code that defines the sql string and does the query.[/color]

          Oops ... I failed to test the semicolon (no error for me either).

          You may want to check the sql error message:

          <?php
          // connect to database
          $sql = "select * from table";
          $res = mysql_query($sq l)
          or die(mysql_error () . ' in [' . $sql . ']');
          // rest of script
          ?>


          --
          I have a spam filter working.
          To mail me include "urkxvq" (with or without the quotes)
          in the subject line, or your mail will be ruthlessly discarded.

          Comment

          • Jan Pieter Kunst

            #6
            Re: SELECT: Syntax error. Please help

            In article <bl91mf$hfq$1@n ews.cybercity.d k>,
            "sam" <rbaba99@carama il.com> wrote:
            [color=blue]
            > FROM users u, orders o[/color]

            users AS u, orders AS o ?

            Just guessing,
            JP

            --
            Sorry, <devnull@cauce. org> is een "spam trap".
            E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

            Comment

            • Andy Hassall

              #7
              Re: SELECT: Syntax error. Please help

              On Mon, 29 Sep 2003 12:35:42 +0200, "sam" <rbaba99@carama il.com> wrote:
              [color=blue]
              >When I run this SQL query:
              >
              >SELECT u.*, o.*
              > FROM users u, orders o
              > WHERE
              > TO_DAYS(o.order _date)
              > BETWEEN
              > TO_DAYS('2003-09-20')-10
              > AND
              > TO_DAYS('2003-09-20')+10
              > AND
              > o.client_id = u.user_id;
              >
              >I get this error:
              >
              >You have an arror in your SQL syntax '' at line 1
              >
              >As you see, this error message have nothing to do
              >with the stucture of my tables or with what they contain.
              >SYNTAX ERROR ??????????????? ????[/color]

              Post the surrounding PHP; are you sure that statement is actually what is
              being executed?

              --
              Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
              Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

              Comment

              • Nikolai Chuvakhin

                #8
                Re: SELECT: Syntax error. Please help

                "sam" <rbaba99@carama il.com> wrote in message
                news:<bl91mf$hf q$1@news.cyberc ity.dk>...[color=blue]
                >
                > When I run this SQL query:
                >
                > SELECT u.*, o.*
                > FROM users u, orders o
                > WHERE
                > TO_DAYS(o.order _date)
                > BETWEEN
                > TO_DAYS('2003-09-20')-10
                > AND
                > TO_DAYS('2003-09-20')+10
                > AND
                > o.client_id = u.user_id;
                >
                > I get this error:
                >
                > You have an arror in your SQL syntax '' at line 1
                >
                > As you see, this error message have nothing to do
                > with the stucture of my tables or with what they contain.
                > SYNTAX ERROR ??????????????? ????[/color]

                Yes, it is a syntax error, right there in the first line of your
                query. If a query is based on more than one table, you can't use
                * as it leads to naming conflicts. To fix this error, you will
                have to explicitly specify all fields you want to obtain, preferably
                with an alias for each field.

                Also, using WHERE o.client_id = u.user_id is commonly considered
                bad style. A better idea is to use o.client_id = u.user_id as
                a join condition.

                So your query should be rewritten along the following lines:

                SELECT
                u.field1 AS u_field1,
                u.field2 AS u_field2,
                u.field3 AS u_field3,
                o.field1 AS o_field1,
                o.field2 AS o_field2,
                o.field3 AS o_field3
                FROM users u LEFT JOIN orders o
                ON o.client_id = u.user_id
                WHERE
                TO_DAYS(o.order _date)
                BETWEEN
                TO_DAYS('2003-09-20')-10
                AND
                TO_DAYS('2003-09-20')+10;

                Cheers,
                NC

                Comment

                • Andy Hassall

                  #9
                  Re: SELECT: Syntax error. Please help

                  On 29 Sep 2003 11:36:52 -0700, nc@iname.com (Nikolai Chuvakhin) wrote:
                  [color=blue]
                  >"sam" <rbaba99@carama il.com> wrote in message
                  > news:<bl91mf$hf q$1@news.cyberc ity.dk>...[color=green]
                  >>
                  >> When I run this SQL query:
                  >>
                  >> SELECT u.*, o.*
                  >> FROM users u, orders o
                  >> WHERE
                  >> TO_DAYS(o.order _date)
                  >> BETWEEN
                  >> TO_DAYS('2003-09-20')-10
                  >> AND
                  >> TO_DAYS('2003-09-20')+10
                  >> AND
                  >> o.client_id = u.user_id;
                  >>
                  >> I get this error:
                  >>
                  >> You have an arror in your SQL syntax '' at line 1
                  >>
                  >> As you see, this error message have nothing to do
                  >> with the stucture of my tables or with what they contain.
                  >> SYNTAX ERROR ??????????????? ????[/color]
                  >
                  >Yes, it is a syntax error, right there in the first line of your
                  >query. If a query is based on more than one table, you can't use
                  >* as it leads to naming conflicts. To fix this error, you will
                  >have to explicitly specify all fields you want to obtain, preferably
                  >with an alias for each field.[/color]

                  No, that's not a syntax error, you just lose fields on the way into PHP.

                  mysql> select t1.*, t2.* from t1, t2;
                  +------+------+
                  | id | id |
                  +------+------+
                  | 1 | 1 |
                  +------+------+
                  1 row in set (0.00 sec)
                  [color=blue]
                  >Also, using WHERE o.client_id = u.user_id is commonly considered
                  >bad style. A better idea is to use o.client_id = u.user_id as
                  >a join condition.
                  >
                  >So your query should be rewritten along the following lines:
                  >
                  >SELECT
                  > u.field1 AS u_field1,
                  > u.field2 AS u_field2,
                  > u.field3 AS u_field3,
                  > o.field1 AS o_field1,
                  > o.field2 AS o_field2,
                  > o.field3 AS o_field3
                  > FROM users u LEFT JOIN orders o
                  > ON o.client_id = u.user_id[/color]

                  Why use LEFT JOIN? Surely you mean [INNER] JOIN; why go for an outer join
                  unless you actually need one?

                  --
                  Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
                  Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

                  Comment

                  • Tom Thackrey

                    #10
                    Re: SELECT: Syntax error. Please help


                    On 29-Sep-2003, nc@iname.com (Nikolai Chuvakhin) wrote:

                    [color=blue][color=green]
                    > > SELECT u.*, o.*
                    > > FROM users u, orders o[/color][/color]
                    (snip)[color=blue]
                    > Yes, it is a syntax error, right there in the first line of your
                    > query. If a query is based on more than one table, you can't use
                    > * as it leads to naming conflicts. To fix this error, you will
                    > have to explicitly specify all fields you want to obtain, preferably
                    > with an alias for each field.[/color]

                    Wrong. u.*, o.* works as coded.

                    --
                    Tom Thackrey

                    Comment

                    • Geoff Berrow

                      #11
                      Re: SELECT: Syntax error. Please help

                      I noticed that Message-ID:
                      <u5%db.95$NW6.2 1104809@newssvr 13.news.prodigy .com> from Tom Thackrey
                      contained the following:
                      [color=blue]
                      >
                      >Wrong. u.*, o.* works as coded.[/color]

                      How is it different from SELECT * ?

                      --
                      Geoff Berrow
                      It's only Usenet, no one dies.
                      My opinions, not the committee's, mine.
                      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                      Comment

                      • Tom Thackrey

                        #12
                        Re: SELECT: Syntax error. Please help


                        On 29-Sep-2003, Geoff Berrow <bl@ckdog.co.uk .the.cat> wrote:
                        [color=blue][color=green]
                        > >
                        > >Wrong. u.*, o.* works as coded.[/color]
                        >
                        > How is it different from SELECT * ?[/color]

                        It allows you to select all the columns from one table (i.e.
                        u.*,o.name,o.pr ice) where * selects all columns from all tables.

                        --
                        Tom Thackrey

                        Comment

                        • Geoff Berrow

                          #13
                          Re: SELECT: Syntax error. Please help

                          I noticed that Message-ID:
                          <Ds1eb.7701$Vl1 .178@newssvr27. news.prodigy.co m> from Tom Thackrey
                          contained the following:
                          [color=blue][color=green][color=darkred]
                          >> >Wrong. u.*, o.* works as coded.[/color]
                          >>
                          >> How is it different from SELECT * ?[/color]
                          >
                          >It allows you to select all the columns from one table (i.e.
                          >u.*,o.name,o.p rice) where * selects all columns from all tables.[/color]

                          I can see that. But in this instance, no difference, surely?

                          --
                          Geoff Berrow
                          It's only Usenet, no one dies.
                          My opinions, not the committee's, mine.
                          Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                          Comment

                          • Tom Thackrey

                            #14
                            Re: SELECT: Syntax error. Please help


                            On 29-Sep-2003, Geoff Berrow <bl@ckdog.co.uk .the.cat> wrote:
                            [color=blue][color=green][color=darkred]
                            > >> >Wrong. u.*, o.* works as coded.
                            > >>
                            > >> How is it different from SELECT * ?[/color]
                            > >
                            > >It allows you to select all the columns from one table (i.e.
                            > >u.*,o.name,o.p rice) where * selects all columns from all tables.[/color]
                            >
                            > I can see that. But in this instance, no difference, surely?[/color]

                            None, but it didn't cause the syntax error either.

                            --
                            Tom Thackrey

                            Comment

                            • sam

                              #15
                              Re: SELECT: Syntax error. Please help

                              Thanks guys for your help,

                              The problem was:
                              My query string contains a ' in the wrong place.

                              THANKS.

                              "sam" <rbaba99@carama il.com> wrote in message
                              news:bl91mf$hfq $1@news.cyberci ty.dk...[color=blue]
                              > When I run this SQL query:
                              >
                              > SELECT u.*, o.*
                              > FROM users u, orders o
                              > WHERE
                              > TO_DAYS(o.order _date)
                              > BETWEEN
                              > TO_DAYS('2003-09-20')-10
                              > AND
                              > TO_DAYS('2003-09-20')+10
                              > AND
                              > o.client_id = u.user_id;
                              >
                              > I get this error:
                              >
                              > You have an arror in your SQL syntax '' at line 1
                              >
                              > As you see, this error message have nothing to do
                              > with the stucture of my tables or with what they contain.
                              > SYNTAX ERROR ??????????????? ????
                              >
                              > Help!
                              >
                              >[/color]


                              Comment

                              Working...