0

I m trying to create ID like "A1 , A2, A3 .. etc So i tried like this

$q = mysql_query("SELECT merge_id FROM merge_info ORDER BY merge_id DESC LIMIT 1;");
$s = mysql_fetch_array($q);
$merge_id5 = $s['merge_id'];
$count2=mysql_num_rows($q);
if($count2>0)
{
    $merge_id5 = explode("A",$merge_id5);   
    $mergeid_no = $merge_id5[1]+1;  
    $merge_id6 = $mergeid_no;
}
else
{
    $merge_id6 ="1";
}
if($count<1)
{
    $merge_id = $merge_id5;
}

Everything is working fine... but after creating A9, it create A10 then again it creates A10 not moving to A11 , A12 . etc., i think if i write correct query to fetch last inserted row i'll fix this issue

Please someone help me

db table :

merge_id  |  name |
      A1  |  xxxx |
      A2  |  yyyy |
      A3  |  zzzz |
        ....
        ....
      A9  |   sds |
      A10 |  dsfs |
4
  • is the merged id always begin with A? Commented Jun 30, 2014 at 7:07
  • 1
    i think better if you add new field that is auto increment Commented Jun 30, 2014 at 7:08
  • @wrecklez yes the merge id should be like this and i dont have to add autoincrement id like 1,2,3,4,.etc :( without using auto increment i have to do this Commented Jun 30, 2014 at 7:15
  • try my posted solution, i know it is not the best solution but it will help you to solve your problem Commented Jun 30, 2014 at 7:52

5 Answers 5

1

i know it is not the best solution to your problem, but this will help you. and just get the first record because if i add LIMIT 1 the output is wrong :(

SELECT * FROM merge_info ORDER BY LENGTH(merge_id) DESC
Sign up to request clarification or add additional context in comments.

3 Comments

it returns A10 but after inserting A11 again it returns A10 not moving to next "A11'
@user3419304 try it now, just get the first record
@user3419304 just remove the limit, then get only the first record in your PHP atleast we found out what is the latest record :) hope it will help, if i got a new and a better solution i will let you know!
0

Change your query to:-

SELECT MAX(merge_id) FROM merge_info;

3 Comments

it returns A9 , but i want A10
change explode to implode. This maght be your solution.
i think there is no need to change any part of coding..! after A9 the the A10 stores after A1 ( A1,A10,A2,A3,A4,A5,A6,A7,A8,A9) so i need to order the table as 1,2,3,4..9,10 then select last row. i dont know how to do that, someone help to solve this issue
0

I think it's better you use an auto_increment field, to get the last insert row with LAST_INSERT_ID(), otherwise you can't be sure if the last row, the one you have inserted.

So long. To order with your solution, use this.

ORDER BY SUBSTRING(merge_id,2) DESC

2 Comments

SELECT merge_id FROM merge_info ORDER BY SUBSTRING( merge_id, 2 ) LIMIT 1 this query returns A1
Use DESC for ORDER, ASC is default
0

Better approach to your problem could be , use AUTO_INCREMENT field and while displaying append 'A' to it.

Now to solve this you can use below SQL statement

SELECT * FROM `merge_info` ORDER BY SUBSTRING(merge_id,2) DESC limit 1

Comments

0

should change you merge_id column into auto increment column,and the change your query into below mentioned,

SELECT MAX(merge_id) FROM merge_info;

if you want id like A1,A2,A3 ... then you can use the below code for get it

 $q = mysql_query("SELECT merge_id FROM merge_info ORDER BY merge_id DESC LIMIT 1;");
                                         $s = mysql_fetch_array($q);
                                         $merge_id5 = $s['merge_id'];
                                         $count2=mysql_num_rows($q);

                    $merge_id="A".$merge_id5;

after above process,noe we have a meger_id like A1,A2.. on application side

1 Comment

Without autoincrement have to do this stuff !! if autoincrement has i do easily !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.