Announcement

Collapse
No announcement yet.

New SQL question.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • New SQL question.

    I ahve the following query that works...

    Code:
    SELECT items.item_name, recipes.*
    FROM `recipes`, `items`
    WHERE items.item_id = recipes.item_id
    AND (recipes.tradeskill = 'Tailoring' OR recipes.tradeskill = 'Smithing')
    AND items.item_name LIKE "%of the skeptic"
    I want to alter that to do an UPDATE on the recipes table and

    SET subcat = 'agnostic'

    can this be done with the cross table nature of it? or do I need to make a script and just get a list of recipe ID's and then run the update on them?
    Ngreth Thergn

    Ngreth nice Ogre. Ngreth not eat you. Well.... Ngreth not eat you if you still wiggle!
    Grandmaster Smith 250
    Master Tailor 200
    Ogres not dumb - we not lose entire city to froggies

  • #2
    Code:
    SELECT items.item_name, recipes.*
    FROM `recipes`, `items`
    WHERE items.item_id = recipes.item_id
    AND (recipes.tradeskill = 'Tailoring' OR recipes.tradeskill = 'Smithing')
    AND items.item_name LIKE "%of the skeptic"
    I'm a gnome, so remember what happened to Luclin after we moved the tinkering headquarters to Shadow Haven.

    Code:
    UPDATE recipes r SET r.subcat = 'agnostic'
    WHERE (r.tradeskill = 'Tailoring' OR r.tradeskill = 'Smithing')
    AND r.item_id in (
        SELECT i.item_id FROM items i WHERE i.item_name LIKE "%of the skeptic"
    )
    You've gotten better at SQL (249)!

    A true expert wouldn't need subselects. For older versions of MySQL, and other implementations without subselects, you may need a temp table.
    I tried combining Celestial Solvent, a Raw Rough Hide, Rough Hide Solution and a Skinning Knife. But the result was such an oxymoron, it opened a rift into another universe. I fell through into one of Nodyin's spreadsheets and was slain by a misplaced decimal.

    Comment


    • #3
      I am game... but.... I think that this version of SQL cannot take sub-selects :/
      Ngreth Thergn

      Ngreth nice Ogre. Ngreth not eat you. Well.... Ngreth not eat you if you still wiggle!
      Grandmaster Smith 250
      Master Tailor 200
      Ogres not dumb - we not lose entire city to froggies

      Comment


      • #4
        yup it choked on it.

        Code:
        #1064 - 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 'SELECT i.item_id FROM items i WHERE i.item_name LIKE "%of the s
        so, looks like I get to write a script

        Thanks for trying!
        Ngreth Thergn

        Ngreth nice Ogre. Ngreth not eat you. Well.... Ngreth not eat you if you still wiggle!
        Grandmaster Smith 250
        Master Tailor 200
        Ogres not dumb - we not lose entire city to froggies

        Comment


        • #5
          From The MySQL news it looks like MySQL has been handling subselects for a couple years now.

          If you've got 4.1 or higher of MySQL, the problem is likely some oddity of the parser that I'm not familiar with. For instance, I notice you quote table names. I've never had to do that. It might help.

          Otherwise, I'm sorry to say a script might be best.
          Last edited by Neebat; 05-09-2005, 11:34 AM.
          I tried combining Celestial Solvent, a Raw Rough Hide, Rough Hide Solution and a Skinning Knife. But the result was such an oxymoron, it opened a rift into another universe. I fell through into one of Nodyin's spreadsheets and was slain by a misplaced decimal.

          Comment


          • #6
            what MySQL ver are you running?

            Comment


            • #7
              MySQL 4.0.22-standard

              and curently it is a shared server, so I cannot get it changed.
              Ngreth Thergn

              Ngreth nice Ogre. Ngreth not eat you. Well.... Ngreth not eat you if you still wiggle!
              Grandmaster Smith 250
              Master Tailor 200
              Ogres not dumb - we not lose entire city to froggies

              Comment


              • #8
                Two quick notes. First, you cannot use IN clauses with MySQL 4.0.22 (as you've already noted). I think IN clauses were introduced with 4.1, but I'm not positive.

                Second, quoting table and field names is optional, but if you do it, you must use the back-quote ` (usually under the tilde, at the top left of the keyboard), NOT the regular apostrophe. I've noticed a lot of times (not in this thread) when people post SQL to help Ngreth, they use the wrong quote sign, and that breaks the code.
                Sir KyrosKrane Sylvanblade
                Master Artisan (300 + GM Trophy in all) of Luclin (Veeshan)
                Master Fisherman (200) and possibly Drunk (2xx + 20%), not sober enough to tell!
                Lightbringer, Redeemer, and Valiant servant of Erollisi Marr

                Comment


                • #9
                  Originally posted by KyrosKrane
                  Two quick notes. First, you cannot use IN clauses with MySQL 4.0.22 (as you've already noted). I think IN clauses were introduced with 4.1, but I'm not positive.
                  You're correct. "IN clauses" are one of the forms of subselects. I wanted to be sure I was giving accurate infromation, so I did a websearch for MySQL subselects. The link I gave above shows that version 4.1 added subselects. (From what I can tell, that includes IN clauses in all reasonable locations.) Since 4.1 has been out for over 2 years, I had some hope that wasn't the problem. Prior to 4.1, MySQL was considered incomplete to the point of unusable by a lot of folks. With that release, it became a viable alternative for large scale projects.

                  Originally posted by KyrosKrane
                  Second, quoting table and field names is optional, but if you do it, you must use the back-quote ` (usually under the tilde, at the top left of the keyboard), NOT the regular apostrophe. I've noticed a lot of times (not in this thread) when people post SQL to help Ngreth, they use the wrong quote sign, and that breaks the code.
                  I avoided the quotes because they looked out of place to me. As far as I know, the only time they're required is table and column names with non-alphanumeric characters. I offered the possibility of adding quotes because I'm always ready for an unfamiliar db engine to do strange things.

                  Sorry, Ngreth. I don't know any way to do your Update in SQL alone with that old MySQL.
                  I tried combining Celestial Solvent, a Raw Rough Hide, Rough Hide Solution and a Skinning Knife. But the result was such an oxymoron, it opened a rift into another universe. I fell through into one of Nodyin's spreadsheets and was slain by a misplaced decimal.

                  Comment


                  • #10
                    Ngreth, try this. According to the SQL docs, this should work. (I'm assuming the "subcat" column only exists in the recipes table, not in items.)

                    Code:
                    UPDATE items, recipes
                    SET subcat = 'agnostic'
                    WHERE items.item_id = recipes.item_id
                    AND (recipes.tradeskill = 'Tailoring' OR recipes.tradeskill = 'Smithing')
                    AND items.item_name LIKE "%of the skeptic"
                    Edit: The reference from the MySQL docs on UPDATE:

                    Starting with MySQL 4.0.4, you can also perform UPDATE operations that cover multiple tables. The table_references part lists the tables involved in the join. Its syntax is described in Section 13.1.7.1, “JOIN Syntax”. Here is an example:

                    UPDATE items,month SET items.price=month.price
                    WHERE items.id=month.id;

                    The example shows an inner join using the comma operator, but multiple-table UPDATE statements can use any type of join allowed in SELECT statements, such as LEFT JOIN.
                    Last edited by KyrosKrane; 05-10-2005, 04:47 PM.
                    Sir KyrosKrane Sylvanblade
                    Master Artisan (300 + GM Trophy in all) of Luclin (Veeshan)
                    Master Fisherman (200) and possibly Drunk (2xx + 20%), not sober enough to tell!
                    Lightbringer, Redeemer, and Valiant servant of Erollisi Marr

                    Comment


                    • #11
                      Originally posted by KyrosKrane
                      Ngreth, try this. According to the SQL docs, this should work. (I'm assuming the "subcat" column only exists in the recipes table, not in items.)

                      Code:
                      UPDATE items, recipes
                      SET subcat = 'agnostic'
                      WHERE items.item_id = recipes.item_id
                      AND (recipes.tradeskill = 'Tailoring' OR recipes.tradeskill = 'Smithing')
                      AND items.item_name LIKE "%of the skeptic"
                      Edit: The reference from the MySQL docs on UPDATE:
                      looks good (I will not be trying this today, to dangerous for my ultra busy work day (every wednesday))

                      but to be more clear could I not do:

                      Code:
                      UPDATE items, recipes
                      SET recipes.subcat = 'agnostic'
                      WHERE items.item_id = recipes.item_id
                      AND (recipes.tradeskill = 'Tailoring' OR recipes.tradeskill = 'Smithing')
                      AND items.item_name LIKE "%of the skeptic"
                      Subcat is unique. but I was wondering if this would deal with the need for it to be unique?
                      Ngreth Thergn

                      Ngreth nice Ogre. Ngreth not eat you. Well.... Ngreth not eat you if you still wiggle!
                      Grandmaster Smith 250
                      Master Tailor 200
                      Ogres not dumb - we not lose entire city to froggies

                      Comment


                      • #12
                        Heh. I misread your post and thought you said the second one didn't work, then spent a goodly bit of time trying to find the problem. =) /bonk Kyros

                        Yes, your change should in theory deal with non-unique column names. But again, I haven't tested it myself.
                        Sir KyrosKrane Sylvanblade
                        Master Artisan (300 + GM Trophy in all) of Luclin (Veeshan)
                        Master Fisherman (200) and possibly Drunk (2xx + 20%), not sober enough to tell!
                        Lightbringer, Redeemer, and Valiant servant of Erollisi Marr

                        Comment


                        • #13
                          Thank you! I really believed you couldn't do an update across two tables, and now I know better.

                          Guess that shows how much SQL I really know.
                          I tried combining Celestial Solvent, a Raw Rough Hide, Rough Hide Solution and a Skinning Knife. But the result was such an oxymoron, it opened a rift into another universe. I fell through into one of Nodyin's spreadsheets and was slain by a misplaced decimal.

                          Comment

                          Working...
                          X