Mysql select question

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

    #16
    Re: Mysql select question

    On 5 Mar 2005 01:44:11 -0800, "frizzle" <phpfrizzle@hot mail.com> wrote:
    [color=blue]
    >But it was too good to be true... I cannot get it to work, and
    >i don't know where to look.. I tried a few things which could possibly
    >somehow make a little bit sense, but they didn't :(
    >
    >I get the following error notice:
    >You have an error in your SQL syntax near 'FROM models m JOIN series s
    >ON (m.series_id = s.series_id AND s.'
    >
    >Please help me, i really don't know where to fix this.
    >
    >(btw, I found it hard to find some good join tuts also)[/color]

    Well, I put together some tables based on what you said and ran it, and with a
    couple of tweaks it at least runs. I'm still not 100% clear on your data as you
    haven't posted any actual examples so it might not do the right thing.

    mysql> desc series;
    +--------+------------------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +--------+------------------+------+-----+---------+-------+
    | id | int(10) unsigned | | PRI | 0 | |
    | active | int(10) unsigned | | | 0 | |
    | name | varchar(24) | YES | | NULL | |
    +--------+------------------+------+-----+---------+-------+
    3 rows in set (0.00 sec)

    mysql> desc models;
    +-----------+------------------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-----------+------------------+------+-----+---------+-------+
    | id | int(10) unsigned | | PRI | 0 | |
    | active | int(10) unsigned | | | 0 | |
    | series_id | int(10) unsigned | | | 0 | |
    | cat_id | int(10) unsigned | | | 0 | |
    | size | varchar(24) | YES | | NULL | |
    +-----------+------------------+------+-----+---------+-------+
    5 rows in set (0.00 sec)

    mysql> insert into series values (1, 1, 'series 1');
    Query OK, 1 row affected (0.03 sec)

    mysql> insert into models values (1, 1, 1, 1, 'big');
    Query OK, 1 row affected (0.02 sec)

    mysql> insert into models values (2, 1, 1, 2, 'small');
    Query OK, 1 row affected (0.01 sec)

    mysql> insert into models values (3, 1, 1, 3, 'medium');
    Query OK, 1 row affected (0.00 sec)

    mysql> select * from series;
    +----+--------+----------+
    | id | active | name |
    +----+--------+----------+
    | 1 | 1 | series 1 |
    +----+--------+----------+
    1 row in set (0.00 sec)

    mysql> select * from models;
    +----+--------+-----------+--------+--------+
    | id | active | series_id | cat_id | size |
    +----+--------+-----------+--------+--------+
    | 1 | 1 | 1 | 1 | big |
    | 2 | 1 | 1 | 2 | small |
    | 3 | 1 | 1 | 3 | medium |
    +----+--------+-----------+--------+--------+
    3 rows in set (0.00 sec)

    mysql> SELECT s.name,
    -> m2.id,
    -> m2.size
    -> FROM models m -- one row for the first product (see WHERE clause at
    end)
    -> JOIN series s -- find the associated series
    -> ON (m.series_id = s.id AND s.active = 1)
    -> JOIN models m2 -- multiple rows for the other products in the series
    -> ON (m2.series_id = s.id
    -> AND m2.id != m.id
    -> AND m2.active = 1)
    -> WHERE m.cat_id = 1
    -> AND m.id = 1
    -> AND m.active = 1;
    +----------+----+--------+
    | name | id | size |
    +----------+----+--------+
    | series 1 | 2 | small |
    | series 1 | 3 | medium |
    +----------+----+--------+
    2 rows in set (0.00 sec)

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • frizzle

      #17
      Re: Mysql select question

      Wow, first of all thousands of thanks for all that work!

      But, weird enough, i tried it with my current tables, and it didn't
      work, then i built
      your examples, but that also didn't work.. :(

      The following query:

      SELECT s.name, m2.id, m2.size
      FROM models m
      JOIN series s ON ( m.series_id = s.id
      AND s.active =1 )
      JOIN models m2 ON ( m2.series_id = s.id
      AND m2.id != m.id
      AND m2.active =1 )
      WHERE m.cat_id =1
      AND m.id =1
      AND m.active =1

      Gives me the following error:
      #1064 - You have an error in your SQL syntax near 'ON ( m.series_id =
      s.id
      AND s.active =1 )
      JOIN models m2 ON ( m2.series_id = ' at line 3

      I know that you've helped me a lot already, but please hgelp me through
      this problem...

      Thanks man!! (If there's anything i can help with, let me know..)

      Thanks!

      Frizzle

      Comment

      • frizzle

        #18
        Re: Mysql select question

        ?

        Comment

        • Andy Hassall

          #19
          Re: Mysql select question

          On 8 Mar 2005 02:09:11 -0800, "frizzle" <phpfrizzle@hot mail.com> wrote:
          [color=blue]
          >?[/color]

          !

          Don't remove all the context when you reply (just because that's what the
          Google Groups Abomination encourages doesn't make it the right way to post to
          Usenet).

          Also have some patience, yes I've not replied in the last 24 hours, but that's
          not surprising since I'm in another timezone and I've been at work. Sheesh.

          --
          Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
          <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

          Comment

          • Andy Hassall

            #20
            Re: Mysql select question

            On 7 Mar 2005 02:54:38 -0800, "frizzle" <phpfrizzle@hot mail.com> wrote:
            [color=blue]
            >But, weird enough, i tried it with my current tables, and it didn't
            >work, then i built
            >your examples, but that also didn't work.. :(
            >
            >The following query:
            >
            >SELECT s.name, m2.id, m2.size
            >FROM models m
            >JOIN series s ON ( m.series_id = s.id
            >AND s.active =1 )
            >JOIN models m2 ON ( m2.series_id = s.id
            >AND m2.id != m.id
            >AND m2.active =1 )
            >WHERE m.cat_id =1
            >AND m.id =1
            >AND m.active =1
            >
            >Gives me the following error:[/color]

            Since it works on the version of MySQL I use (4.1), you are clearly on a
            different version. Which version?

            Did you check the manual? It's at http://dev.mysql.com/doc/mysql/en/index.html
            and is searchable.
            [color=blue]
            >#1064 - You have an error in your SQL syntax near 'ON ( m.series_id =
            >s.id
            >AND s.active =1 )
            >JOIN models m2 ON ( m2.series_id = ' at line 3[/color]

            OK, so from the error it's complaining about the "ON" clause. This is
            immediately after a JOIN, so perhaps it's that. So put "JOIN" in the search box
            in the manual. The first hit is section 13.1.7.1. JOIN Syntax:

            http://dev.mysql.com/doc/mysql/en/join.html

            Read through that. It points out:

            "Note that INNER JOIN syntax allows a join_condition only from MySQL 3.23.17
            on. The same is true for JOIN and CROSS JOIN only as of MySQL 4.0.11."

            So, you're using either MySQL 3.23 or a version of 4.0 earlier than 4.0.11 if
            it doesn't accept a join condition for JOINs.

            Replace "JOIN" with "INNER JOIN" in the SQL above. If that doesn't work you're
            on some extremely old version of MySQL and should probably either upgrade or
            abandon all hope.

            --
            Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
            <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

            Comment

            • frizzle

              #21
              Re: Mysql select question

              My sincere apologies Andy,

              I didn't mean to look impatient or anything else/worse..

              I really appreciate your help. I'm quite new to PHP, and especially
              to mySQL, so all the help i get is welcome, and i wouldn't want to ruin
              it by appearing impatient....

              Anyway, i've checked my phpinfo() and i read that my php-version is
              PHP Version 4.3.2, so that should be no problem...

              Thanks again for helping me out. I'll try these new things tomorrow.
              First i have to get some sleep... zzzzzz

              Frizzle

              Comment

              • frizzle

                #22
                Re: Mysql select question

                Too bad, tried the inner join thing, raed the mySQL docs,
                but can't figure it out, it still gives me the same error.... :( :

                You have an error in your SQL syntax near 'ON (m.series_id=s. id AND
                s.active=1) JOIN models m2 ON ('

                I even tried to add quotes to the '1' etc. but nothing seems to work...

                Frizzle

                Comment

                • Andy Hassall

                  #23
                  Re: Mysql select question

                  On 9 Mar 2005 04:47:36 -0800, "frizzle" <phpfrizzle@hot mail.com> wrote:
                  [color=blue]
                  >Too bad, tried the inner join thing, raed the mySQL docs,
                  >but can't figure it out, it still gives me the same error.... :( :
                  >
                  >You have an error in your SQL syntax near 'ON (m.series_id=s. id AND
                  >s.active=1) JOIN models m2 ON ('[/color]

                  The fragment of SQL from the error message shows that you haven't actually
                  changed the JOINs to INNER JOINs.

                  --
                  Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
                  <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

                  Comment

                  • frizzle

                    #24
                    Re: Mysql select question

                    Indeed, not in the error i pasted, but i've tried it actually, and it
                    gave me exactly the same error...

                    Comment

                    Working...