autoincrementable column with letter and numbers depending on another column

0

I want to create an autoincrementable column that recognizes the type of voucher that will be issued. I have the following table:

-------------------------------------------------------------
|id  | numcomprobante  | tipocomprobante  | iddetalleventa   |
-------------------------------------------------------------
| 1  | B00-0001        | BOLETA           | 1                |
| 2  | B00-0002        | BOLETA           | 2                |
| 3  | F00-0001        | FACTURA          | 3                |
| 4  | B00-0003        | BOLETA           | 4                |

As you can see, the first two rows that are ballots are issued in incremental order, but with a prefix of letter B, like the initial of the type of voucher, however the third row restarts the number, and the letter prefix change to another letter 'F', then continue with the previous number

How could I do it with a trigger?

    
asked by Raphael 15.12.2017 в 07:09
source

1 answer

-1
public function registrar($tipocomprobante,$iddetalleventa)
                     {
                      $sql='SELECT MAX(id) FROM tabla';
                      $resultado = $this->consultar($sql);

                      $numcomprobante="";
                      if($tipocomprobante=="BOLETA"){
                        $numcomprobante="B00-00$resultado+1";
                      }  else {
                        $numcomprobante="F00-00$resultado+1";
                     }

                    $sw = true;
                    $sql= 'INSERT INTO tabla  
                   (numcomprobante,tipocomprobante,iddetalleventa,) 
                    VALUES (  \''.$numcomprobante.'\', 
                   \''.$tipocomprobante.'\', 
                   \''.$iddetalleventa.'\')';


                   $this->conexion->query($sql) or $sw = false;
                   return $sw;
                   }
    
answered by 16.12.2017 в 00:21