SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Problems with SQL statement, on duplicate key update

    Hello!

    I am trying to update a record or insert it if it does not exist but I am having a hard time doing it and I canīt find where the problem is, can someone help me out and point me in the right direction

    Code:
    INSERT INTO `jml_fl_users` 
    	(
    		`id`, 
    		`street`, 
    		`town`, 
    		`city`, 
    		`state`, 
    		`country`, 
    		`zipcode`, 
    		`coordinates`, 
    		`phone`, 
    		`website`, 
    		`favoritebook`, 
    		`aboutme`, 
    		`birthdate`
    	) 
    	VALUES(
    		54, 
    		'200 Cisnes', 
    		'Lago de Guadalupe', 
    		'Cuautitlán Izcalli', 
    		'Mexico', 
    		'Mexico', 
    		'54760', 
    		'19.6285952093597,-99.25037122265627', 
    		'6148356117', 
    		'http://www.tlacaelelrl.com', 
    		'gttry', 
    		'rtyrty', 
    		'1983-01-04'
    	)
    	ON DUPLICATE KEY UPDATE
    	SET street = '200 Cisnes', 
    	town = 'Lago de Guadalupe', 
    	city = 'Cuautitlan Izcalli',
    	state = 'Mexico',
    	country = 'Mexico',
    	zipcode = '54760', 
    	coordinates = '19.6285952093597,-99.25037122265627',
    	phone = '6148356117',
    	website = 'http://www.tlacaelelrl.com', 
    	favoritebook = 'gttry', 
    	aboutme = 'gttry', 
    	birthdate = '1983-01-04'
    	WHERE 
    	id = 54;
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  2. #2
    SitePoint Wizard webcosmo's Avatar
    Join Date
    Oct 2007
    Location
    Boston, MA
    Posts
    1,480
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    why not do the check first.

    If Not Exists(select (1) from ...........Where....)
    Insert into ............

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,241
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by tlacaelelrl View Post
    ... I canīt find where the problem is, can someone help me out and point me in the right direction
    you know, you really should become friends wif da manual

    look up the INSERT statement for the correct syntax

    here, i'll help you this time... http://dev.mysql.com/doc/refman/5.1/en/insert.html

    there is no SET keyword allowed in the ON DUPLICATE KEY clause, and no WHERE clause either

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

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