logo
Tags down

shadow

mysql insert into existing row


By : Sameer Bhagyawant
Date : May 05 2020, 08:16 AM
I trying to add the suffixes to the older (existing) value(s) in the table.

Hence I would have this result:

enter image description here

And if I need to insert dude again, the trigger will check if it exists, then append the next available (-3 in this case) to the existing one, and insert the newest dude WITHOUT a suffix:

enter image description here

Is this possible to do that ?

Thanks in advance.

Answer :

A trigger in MySQL can't act on mysql table which caused it to fire, hence your plan will not work, at least not as you described it. What you are asking for is going to be very difficult, if not impossible, to do, and would probably end up being a maintenance nightmare. I actually don't see any point to labelling the slug values with versions. I can offer a workaround, namely just building the version suffix at the time you query. This is fairly painless, and frees you from having a draconian table design. Something like this:

SET @rank = 0;
SET @slug = NULL;

SELECT
    t1.id,
    CASE WHEN t2.id IS NULL
         THEN CONCAT(t1.slug, '-', CAST(t1.label AS CHAR(50)))
         ELSE t1.slug END AS slug
FROM
(
    SELECT
        id,
        @rank:=CASE WHEN @slug = slug THEN @rank + 1 ELSE 1 END AS label,
        @slug:=slug AS slug
    FROM yourTable
    ORDER BY slug, id
) t1
LEFT JOIN
(
    SELECT slug, MAX(id) AS id
    FROM yourTable
    GROUP BY slug
) t2
    ON t1.id   = t2.id AND
       t1.slug = t2.slug;

Output:

enter image description here

Demo here:

Rextester



Share : facebook icon twitter icon
Related Posts Related Posts :
  • clear array after form submit
  • pass url variable to javascript
  • access data using api
  • div position fixed css
  • add horizontal line between two div
  • disable toggle button css
  • css fixed line height
  • background image not working
  • table cell border
  • text line break css
  • overlay div on div
  • text direction rtl css
  • javascript convert string to an object
  • bind inner div click to outer div
  • jquery ajax oncomplete
  • how to use promise in for loop
  • jquery get element using variable
  • javascript send textbox value
  • pass div class content to another class or id
  • css new line after element
  • css calc not working properly
  • link disabled jquery
  • sql join table to subquery
  • MS Access SQL Issue with OR AND Operators
  • set textarea value using php
  • php merge two arrays into one
  • php form with google captcha
  • update current row in php
  • php date format mysql
  • mysql pdo select query
  • php function return an array
  • PHP mysqldump database to sql file
  • display image query
  • php create array using foreach loop
  • associative array php
  • group_concat in mysql laravel
  • php array_push empty
  • mysql on update cascade not working
  • pdo insert prepared statement
  • mysql insert ignore vs on duplicate key
  • mysql multi insert query
  • mysql group by show all rows
  • php change profile image
  • combine multiple arrays into one array php
  • how to sum values in an array
  • how to get sum of the total time
  • How to Create Facebook live stream without notification using API ?
  • on button click make div visible
  • css affect another element on hover
  • insert line after paragraph word
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org