简体   繁体   English

将枚举转换为int并从MySQL的另一个表插入该int值

[英]Cast enum to int and insert that int value from another table in MySQL

I am Converting my Enum datatype to Integer and Also Want to Insert that perticular id to int value which in another table. 我正在将我的Enum数据类型转换为Integer ,并且还想将该垂直ID插入另一个表中的int值。

Enum Type in product_mst product_mst中的 枚举类型

measurement_unit: Enum('pair','piece') and Default value is 'pair' 

When i will convert it to integer its id should be lookup in another table. 当我将其转换为整数时,其ID应该在另一个表中查找。

Another Table that is measurement_unit_mst 另一个表是measurement_unit_mst

measurement_unit_id | unit_name |
        5              pair
        8              piece

It should be 5 for pair and also 8 for piece, when i convert it to integer . 当我将其转换为integer时,它应该是5对,也应该是8对。

Normally, it takes 1 and 2 for 'pair' and 'piece' that i don't want it. 通常,我不想要“对”和“件”需要1和2。

I applied below three query which working for me. 我下面的三个查询适用于我。

ALTER TABLE `product_mst` CHANGE `measurement_unit` `measurement_unit` INT(11) NULL DEFAULT '0';
UPDATE product_mst set measurement_unit=5 where measurement_unit=1;
UPDATE product_mst set measurement_unit=8 where measurement_unit=2;

Any other Better option for this? 还有其他更好的选择吗?

Thank You. 谢谢。

If your data table is the central part in your design, then it might be better to initalize the enum with the corresponding numbers of the table so you can omit the conversion: 如果数据表是设计的中心部分,那么最好用表的相应编号来初始化枚举,这样就可以省略转换:

abstract class MeasurementUnit
{
    const pair = 5;
    const piece = 8;
    // etc.
}

Access like 访问方式

$pair = MeasurementUnit::pair;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM