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 ;

506 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
  113. Pingback: casual encounters
  114. Pingback: peliculas online
  115. Pingback: porn download
  116. Pingback: skoleni jerabniku
  117. Pingback: Cenforce 150mg
  118. Pingback: best clit toys
  119. Pingback: room tour
  120. Pingback: ăn trưa ngon q10
  121. Pingback: today jaffna
  122. Pingback: seo publissoft
  123. Pingback: خرید vpn
  124. Pingback: beginner dong
  125. Pingback: breakfast near me
  126. Pingback: Mesothelioma
  127. Pingback: roblox games
  128. Pingback: new sex toy
  129. Pingback: descargasmix
  130. Pingback: dildo challenge
  131. Pingback: dildo sex toy
  132. Pingback: asmr sex toy
  133. Pingback: compare flights
  134. Pingback: blackhat seo links
  135. Pingback: asmr sex toys
  136. Pingback: garden seeds
  137. Pingback: womens boots
  138. Pingback: katmovies
  139. Pingback: Buy pbn links
  140. Pingback: 메이저사이트
  141. Pingback: 우리카지노
  142. Pingback: 코인카지노
  143. Pingback: 예스카지노
  144. Pingback: flight radar
  145. Pingback: asmr sex toys
  146. Pingback: VIAGRA
  147. Pingback: web designing
  148. Pingback: viagra online
  149. Pingback: CBD oil for pain
  150. Pingback: zwrot prowizji
  151. Pingback: the bullet sex toy
  152. Pingback: fingertip massager
  153. Pingback: seo hk
  154. Pingback: thrusting vibe
  155. Pingback: Houses for sale
  156. Pingback: roblox
  157. Pingback: realistic dong
  158. Pingback: male masturbator
  159. Pingback: male p spot toys
  160. Pingback: torrent download
  161. Pingback: VIAGRA
  162. Pingback: laws
  163. Pingback: Website analysis
  164. Pingback: FREE SEX VIDEOS
  165. Pingback: VIRUS DETECTED
  166. Pingback: dildo vibrator
  167. Pingback: how to use a dildo
  168. Pingback: biography wiki
  169. Pingback: silicone vibrator
  170. Pingback: FREE XXX VIDEO
  171. Pingback: sex swings
  172. Pingback: how to find g spot
  173. Pingback: Naija
  174. Pingback: top vibrators
  175. Pingback: puppy trainer
  176. Pingback: SSNI-661
  177. Pingback: terjemahan
  178. Pingback: VIAGRA
  179. Pingback: VePorn.com
  180. Pingback: خرید vpn
  181. Pingback: 더킹카지노
  182. Pingback: 코인카지노
  183. Pingback: CCRMG
  184. Pingback: HACK PORN CRACK
  185. Pingback: suction cup dong
  186. Pingback: free sexy
  187. Pingback: Jumpstart car
  188. Pingback: FCMB Mobile App
  189. Pingback: Google
  190. Pingback: p spot play
  191. Pingback: clit vibrator
  192. Pingback: LEVITRA CHEAP
  193. Pingback: LSD DRUGS
  194. Pingback: whips for sale
  195. Pingback: best lube for anal
  196. Pingback: sexy wear
  197. Pingback: vector art service
  198. Pingback: ketamine crystals
  199. Pingback: suction massager
  200. Pingback: vibrating strapon
  201. Pingback: kurtki z alpaki
  202. Pingback: prepaidgiftbalance
  203. Pingback: ترمیم مو
  204. Pingback: netprobk
  205. Pingback: best cbd capsules
  206. Pingback: Age
  207. Pingback: enamel mug set
  208. Pingback: nileriver
  209. Pingback: Umzugskosten
  210. Pingback: msn hotmail
  211. Pingback: جت هیتر
  212. Pingback: money management
  213. Pingback: vibrating bullet
  214. Pingback: best cbd gummies
  215. Pingback: University
  216. Pingback: Anambrablog.ng
  217. Pingback: Download songs
  218. Pingback: female sex toys
  219. Pingback: sous traitance web
  220. Pingback: dildo
  221. Pingback: sous traitance web
  222. Pingback: cbd oil
  223. Pingback: invest island scam
  224. Pingback: THE CAMERA GUYS
  225. Pingback: realistic dildos
  226. Pingback: seo manchester
  227. Pingback: cupooftea
  228. Pingback: "buy ephedra"
  229. Pingback: pheromone spray
  230. Pingback: Free Ads UK
  231. Pingback: 4chan porn
  232. Pingback: xnxx
  233. Pingback: click here
  234. Pingback: Vape shop online
  235. Pingback: THC Cartridge
  236. Pingback: Hydrocodone
  237. Pingback: Game Poker Seru
  238. Pingback: macys insite
  239. Pingback: sapphire lawn 2020
  240. Pingback: viagra
  241. Pingback: viagra
  242. Pingback: best CBD oil
  243. Pingback: original post
  244. Pingback: Weed for sale
  245. Pingback: THC Oil
  246. Pingback: reshpcos lua dao
  247. Pingback: buy cbd gummies
  248. Pingback: cannabidiol
  249. Pingback: pure cbd
  250. Pingback: tm menards
  251. Pingback: SEO
  252. Pingback: fda food facility
  253. Pingback: samovar nyc
  254. Pingback: wand massager
  255. Pingback: smoke shop near me
  256. Pingback: SMM reseller panel
  257. Pingback: gadgets cool stuff
  258. Pingback: india visa

Leave a Reply