1

So I have a table of items as so

|mainCtgry |subCtgry|
---------------------
|     1    |   1    |
---------------------
|     1    |   2    |
---------------------
|     1    |   3    |
---------------------
|     2    |   1    |
---------------------
|     2    |   2    |
---------------------
|     3    |   1    |
---------------------
|     4    |   1    |
---------------------

So mainCtgry and subCtgry are the primary keys, you can't see it in the scheme but both are related to a string column let's say mainStr and subStr

My question is how can I auto-increment subCtgry based on the last subCtgry value related to mainCtgry (so subStr). Let be more clear if I enter for exemple cat into mainStr and generic into subStr then mainCtgry will be 1 and subCtgry will be 1, now if I enter cat into mainStr and Birman into subStr I would like my mainCtgry to still be 1 and subCtgry to be 2

Is this possible to automate this kind of behaviours in MYSQL?

4
  • Have look here: stackoverflow.com/questions/52003972/… (Note: The question eventually turned out to be on SQL Server (not tagged properly before) but the answer I linked is for MySQL.)
    – sticky bit
    Aug 25 '18 at 21:16
  • thanks but as I don't really understand MYSQL propoerly I would prefer having an example just for my case just to be sure not to get lost with other examples. But thanks anyway I'll have a look and btw I'm not sure to be using the same engine as in the link you've sent me.
    – JSmith
    Aug 25 '18 at 21:21
  • There is some discussion here
    – Rick James
    Sep 20 '18 at 20:23
  • @RickJames ok thanks I've changed the way of thinking for my DB but anyway thanks for the help
    – JSmith
    Sep 20 '18 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.