1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

MySQL UPDATE Trouble

Discussion in 'MySQL' started by T0PS3O, Jul 22, 2005.

  1. #1
    What's wrong with this?

    UPDATE products, products_description SET products.vendors_id = '2' WHERE products.products_id = products_description.products_id AND products_description.products_name LIKE 'something%'

    I basically want to update a record (vendors_id) in table A (products) if a corresponding record (products_name) in table B (products_description) starts with 'something'.

    I keep getting this: You have an error in your SQL syntax near ' products_description SET products.vendors_id = '2' WHERE products.products_id =' at line 1

    If I delete products_description after UPDATE products, it complaints it's an unknown table.

    Any guru's know a fix?
     
    T0PS3O, Jul 22, 2005 IP
  2. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #2
    You can't have 2 tables after in update like that you will need to use a sub query, something like
    
    update products set products.vendors_id=2
    where
    products.product_id = (select products_id from product_descriptions 
                                   where products_description.product_name 
                                    LIKE 'something%')
    
    Code (markup):
    Not tested at all but it should give you an idea
     
    dct, Jul 22, 2005 IP
  3. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks!

    Tried loads of variations of that but to no avail. Will have to dig and actually understand first I guess.

    Could the sub query be expecting just one result? Mine will return 40 odd rows. On its own it works fine.
     
    T0PS3O, Jul 22, 2005 IP
  4. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #4
    oh in that case use in instead of =
    Also in the sub query I missed an s (product_desciptions) in the where clause
     
    dct, Jul 22, 2005 IP
  5. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yeah I fixed the typo's. Where do you think should 'in' go? Never heard of that.

    Documentation says:

     
    T0PS3O, Jul 22, 2005 IP
  6. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #6
    update products set products.vendors_id=2
    where
    products.product_id in (select products_id from product_descriptions
    where product_descriptions.product_name
    LIKE 'something%')
     
    dct, Jul 22, 2005 IP
  7. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Well, still no luck.

    Same with:

     
    T0PS3O, Jul 22, 2005 IP
  8. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Both queries work independantly so no typo's or whatever...
     
    T0PS3O, Jul 22, 2005 IP
  9. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #9
    Try
    
    UPDATE products
    SET vendors_id = '2' 
    WHERE products_id in 
    	(SELECT products_id
    	FROM products_description
    	WHERE products_name
    	LIKE 'something%')
    
    Code (markup):
    I've tested the same syntax in MS SQL on a different table and it works fine
     
    dct, Jul 22, 2005 IP
  10. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I tried that one earlier, no table names, and it still doesn't work :(

    Almost starting to think it's phpMyAdmin's fault.

    What do you run it on?

    Mine is phpMyAdmin 2.3.2 on MySQL 3.23.58
     
    T0PS3O, Jul 22, 2005 IP
  11. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #11
    This is the first cutdown subquery I can get to work though it's not a real one:

    SELECT *
    FROM products
    WHERE products_id = ( 1 + 63 ) LIMIT 0, 30

    But this doesn't work (I know it's a silly one but just to prove my point):

    SELECT *
    FROM products
    WHERE products_id = (
    SELECT products_id
    FROM products_description
    WHERE products_id = '64' )

    Getting annoyed now...

    Apprciate your dedication in helping me out!
     
    T0PS3O, Jul 22, 2005 IP
  12. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #12
    It definetly should work, I've just tried it on 4.1.12 and no problems at all, I'm not using PHPmyAdmin but the mySQL Query Analyser program.

    Sub queries are common SQL features so would be surprised if any version didn't support them, not sure what else to suggest here. Can you try it from a PHP script or better stil Query Analyzer and see if you get better error messages.
     
    dct, Jul 22, 2005 IP
  13. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #13
    dct, Jul 22, 2005 IP
  14. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Bollocks, what a waste of time. Any old school methods for me to do this (BTW in the time pissing about with this I could have updated all records manually, doh!)?
     
    T0PS3O, Jul 22, 2005 IP
  15. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #15
    2 Choices:
    Upgrade - Adviced
    Write a PHP script so that it dynamicly creates the where clause from the sub query, it should look like
    
    where product_id in (1, 2, 3, 4)
    
    Code (markup):
    I'm off home now so won't be able to provide any more info for an hour or so, good luck
     
    dct, Jul 22, 2005 IP
    T0PS3O likes this.
  16. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Option 1: Shared Host - No Go (Will move to dedicated higher spec later this year)
    Option 2: Never mind, just finished it manually.

    Expect some green :)
     
    T0PS3O, Jul 22, 2005 IP
  17. jimrthy

    jimrthy Guest

    Messages:
    283
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #17
    That sounds like a miserable time!

    I never really feel safe using sub queries. For something small like this, I'll do the select, put the results into a list, and then loop through that to do the updates.

    Not that that's really an option if you have lots of records involved.

    But I'd still rather write code than run updates manually. Sorry you had such a nightmare. I definitely feel your pain.
     
    jimrthy, Jul 24, 2005 IP
  18. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Thanks. Luckily it only involved about 40 records and I found a way to semi-automate it so in the ned I only had to manually adjust half a dozen or so.

    I'll defo look out for MySQL 4.whatever on the next server!
     
    T0PS3O, Jul 24, 2005 IP