mysql全局定义_mysql自定义全局定义变量

bd96500e110b49cbb3cd949968f18be7.png

In my database design, I tend to store some variable that is meant to be acting as a ROLE or TYPE as SMALLINT. For example :

CREATE TABLE `house` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`type` smallint(11) NOT NULL,

And in php, I do

define('HOUSE_SMALL_TYPE', '0');

define('HOUSE_MEDIUM_TYPE', '1');

So in php, in SELECT queries I do :

$this->db->query("SELECT * FROM house

WHERE type=?;", HOUSE_SMALL_TYPE);

My questions are :

In the php part, is there is a better way to do this ?

In the mysql itself, does mysql also has global define functionality (like the define in php) ?

I also want to do kind of

SELECT * FROM house WHERE type = HOUSE_SMALL_TYPE

in mysql query.

My purpose is that when I do SELECT in mysql, no way I'm going to keep mapping the value 0,1,2 with its real meaning. Just convineance for viewing the tables values, without changing the structure table and fields.

解决方案

I suggest using MySQL variables:

SET HOUSE_SMALL_TYPE = 0;

SET HOUSE_MEDIUM_TYPE = 1;

Then, in your queries you may use these variables:

SELECT * FROM house WHERE type = @HOUSE_SMALL_TYPE;

This method defines session variables:

If you change a session system variable, the value remains in effect

until your session ends or until you change the variable to a

different value. The change is not visible to other clients.

If you want to define global MySQL variables (available to all sessions):

SET GLOBAL HOUSE_SMALL_TYPE = 0;

SET GLOBAL HOUSE_MEDIUM_TYPE = 1;

To indicate explicitly that a variable is a global variable, precede

its name by GLOBAL or @@global.. The SUPER privilege is required to

set global variables.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值