0

i created a student id number compose of text and auto incremented value like: KP0001 - KP9999 it will reset to KP0001 again, It will result to duplicate ID number. I changed it to this: 2013-00001 but it's not working i dont know why.

this is what i tried:

text  id
KP    1
KP    2
KP    3

SELECT * ,concat(text,LPAD(id,4,'0')) as stud_id FROM students

i want to change the text into year which will update yearly. Thanks, Mills

Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
user2089597
  • 85
  • 3
  • 12

1 Answers1

0

Try this

SELECT
  *,
  concat(YEAR(NOW()),'-',LPAD(id,5,'0')) as stud_id
FROM students

SQL Fiddle Demo

OUTPUT

id  text    stud_id
--------------------
1   KP      2013-00001
2   KP      2013-00002
3   KP      2013-00003

You can use this update query

UPDATE students AS s
LEFT JOIN(
        SELECT 
        *
        FROM students
    ) AS l ON l.id = s.id
SET s.text = concat(YEAR(NOW()),'-',LPAD(l.id,5,'0'))
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103