Update if record already exist else insert-MySQL

I am uploading an excel file into the database table,

What I want is, If any record from excel sheet already exists into the table then update that record with the latest data which is available on the sheet.

How to do that?

Well,

It is pretty simple

Following is the syntax to perform that query:

IF EXISTS (SELECT Col1,Col2,Col3 FROM TheTable WHERE Col1 = ExcelCol AND Col2 = ExcelCol2 AND ....)
  BEGIN
    --Update Query
  END
ELSE
  BEGIN
    -- Insert Query
  END
/*Here is the complete example using stored Procedure*/
DELIMITER $$

USE `db_name`$$

DROP PROCEDURE IF EXISTS `upd_if_exist`$$

CREATE DEFINER=`admin`@`%` PROCEDURE `upd_if_exist`(
  Vid_name VARCHAR (1000),
  Vid_number VARCHAR(200),
  Vcity VARCHAR (1000),
  Vprovince VARCHAR (1000),
  Vevent_date VARCHAR(200),
  Vstart_time VARCHAR(200),
  Vend_time  VARCHAR(200),
  Vplace VARCHAR (2000),
  Vaddress VARCHAR(2000))
   
COMMENT 'Add or Edit Event'
    
BEGIN
IF EXISTS(SELECT id_number,event_date FROM all_events WHERE id_number=Vid_number AND event_date=Vevent_date) THEN	
  UPDATE power_tour_events SET 
  id_name=Vid_name,city=Vcity,province=Vprovince,start_time=Vstart_time,end_time=Vend_time,place=Vplace,
  address=Vaddress WHERE (id_number=Vid_number AND event_date=Vevent_date);
  ELSE
  INSERT INTO `power_tour_events` (
    dealer_name,
    dealer_number,
    city,
    province,
    event_date,
    start_time,
    end_time,
    place,
    address
  )
  VALUES
    (
      Vdealer_name,
      Vdealer_number,
      Vcity,
      Vprovince,
      Vevent_date,
      Vstart_time,
      Vend_time,
      Vplace,
      Vaddress);
END IF;
 END$$

DELIMITER ;

172 thoughts on “Update if record already exist else insert-MySQL

  1. Pingback: Abzocke
  2. Pingback: English Bread
  3. Pingback: thatstamil
  4. Pingback: Goedkoop Taxi
  5. Pingback: online business
  6. Pingback: chip card pos
  7. Pingback: kona coffee
  8. Pingback: kona coffee
  9. Pingback: kona coffee
  10. Pingback: kona coffee
  11. Pingback: kona coffee
  12. Pingback: biyougeka
  13. Pingback: empanada wikipedia
  14. Pingback: Phone cases
  15. Pingback: beauty tips
  16. Pingback: vape starter kit
  17. Pingback: modern designer
  18. Pingback: Mental health
  19. Pingback: Bachatero
  20. Pingback: bts merchandise
  21. Pingback: canal infantil
  22. Pingback: PD Blower
  23. Pingback: medium
  24. Pingback: czyszczenie strony
  25. Pingback: Games golf
  26. Pingback: eBay Paraguay
  27. Pingback: game of Thrones
  28. Pingback: php scripts
  29. Pingback: buy bikinis
  30. Pingback: Business ideas
  31. Pingback: TV mounting
  32. Pingback: Steuerberatung
  33. Pingback: kona coffee store
  34. Pingback: kona coffee brands
  35. Pingback: pure kona coffee
  36. Pingback: kona coffee
  37. Pingback: kona coffee beans
  38. Pingback: 100% kona coffee
  39. Pingback: 100% kona coffee
  40. Pingback: 100% kona coffee
  41. Pingback: Musica chilena
  42. Pingback: luck spells
  43. Pingback: hadith for kids
  44. Pingback: hadith for kids
  45. Pingback: Joomla extensions
  46. Pingback: custom patches
  47. Pingback: ananda
  48. Pingback: Lugares Turisticos
  49. Pingback: Groomer
  50. Pingback: cbd öl vergleich
  51. Pingback: individual freedom
  52. Pingback: gym junky
  53. Pingback: data logger
  54. Pingback: mt4 Indicators
  55. Pingback: measure your size
  56. Pingback: Affordable movers
  57. Pingback: seo agency
  58. Pingback: makeup artist
  59. Pingback: catering
  60. Pingback: Mexico Realestate
  61. Pingback: kona beans
  62. Pingback: Fashion Week
  63. Pingback: vacations
  64. Pingback: best
  65. Pingback: dumpling Boston
  66. Pingback: marketing viral
  67. Pingback: bad credit loans
  68. Pingback: Champions
  69. Pingback: Clean Clothes
  70. Pingback: Cosplay Tattoo
  71. Pingback: kona bean coffee
  72. Pingback: pure kona
  73. Pingback: Trusted
  74. Pingback: Used lab equipment
  75. Pingback: growth mindset
  76. Pingback: T-REX Mural
  77. Pingback: cat props
  78. Pingback: #bruhtawk
  79. Pingback: geologyrocks
  80. Pingback: Managed Databases
  81. Pingback: Jewelry
  82. Pingback: stem
  83. Pingback: deck builder
  84. Pingback: moving services
  85. Pingback: epoxy garage floor
  86. Pingback: Project
  87. Pingback: Process servers
  88. Pingback: iphone repair
  89. Pingback: vip women
  90. Pingback: Arabic Proverbs
  91. Pingback: Google
  92. Pingback: 메이저놀이터
  93. Pingback: movies
  94. Pingback: VIAGRA
  95. Pingback: legal
  96. Pingback: Plus
  97. Pingback: Professionnel
  98. Pingback: 먹튀검증
  99. Pingback: 메이저사이트
  100. Pingback: 메이저사이트
  101. Pingback: cbd oil
  102. Pingback: enlargement pump
  103. Pingback: 먹튀검증
  104. Pingback: 우리카지노
  105. Pingback: viagra pills
  106. Pingback: 황금성
  107. Pingback: stiiizy
  108. Pingback: Sauna
  109. Pingback: SeventyOne
  110. Pingback: MVSD 410
  111. Pingback: clé
  112. Pingback: wand vibrator

Leave a Reply