MySQL, Concatenate two columns

后端 未结 4 1365
醉酒成梦
醉酒成梦 2020-11-28 08:44

There are two columns in a MySQL table: SUBJECT and YEAR.

I want to generate an alphanumeric unique number which holds the concatenated da

相关标签:
4条回答
  • 2020-11-28 08:45

    In php, we have two option to concatenate table columns.

    First Option using Query

    In query, CONCAT keyword used to concatenate two columns

    SELECT CONCAT(`SUBJECT`,'_', `YEAR`) AS subject_year FROM `table_name`;
    

    Second Option using symbol ( . )

    After fetch the data from database table, assign the values to variable, then using ( . ) Symbol and concatenate the values

    $subject = $row['SUBJECT'];
    $year = $row['YEAR'];
    $subject_year = $subject . "_" . $year;
    

    Instead of underscore( _ ) , we will use the spaces, comma, letters,numbers..etc

    0 讨论(0)
  • 2020-11-28 08:53

    You can use php built in CONCAT() for this.

    SELECT CONCAT(`name`, ' ', `email`) as password_email FROM `table`;
    

    change filed name as your requirement

    then the result is

    and if you want to concat same filed using other field which same then

    SELECT filed1 as category,filed2 as item, GROUP_CONCAT(CAST(filed2 as CHAR)) as item_name FROM `table` group by filed1 
    

    then this is output

    0 讨论(0)
  • 2020-11-28 08:54

    You can use the CONCAT function like this:

    SELECT CONCAT(`SUBJECT`, ' ', `YEAR`) FROM `table`
    

    Update:

    To get that result you can try this:

    SET @rn := 0;
    
    SELECT CONCAT(`SUBJECT`,'-',`YEAR`,'-',LPAD(@rn := @rn+1,3,'0'))
    FROM `table`
    
    0 讨论(0)
  • 2020-11-28 09:03
    $crud->set_relation('id','students','{first_name} {last_name}');
    $crud->display_as('student_id','Students Name');
    
    0 讨论(0)
提交回复
热议问题