本文主要向大家介绍了mysql数据库之mysql的binary类型操作,通过具体的内容向大家展现,希望对大家学习mysql数据库有所帮助。
示例数据表:
create table test_bin (
bin_id binary(16) not null
) engine=innodb;
插入数据(内容是一个32位的uuid字符串值):
insert into test_bin(bin_id) values(unhex(‘fa34e10293cb42848573a4e39937f479‘));
insert into test_bin(bin_id) values(unhex(?));
或
insert into test_bin(bin_id) values(x‘fa34e10293cb42848573a4e39937f479‘);
查询数据:
select hex(bin_id) as bin_id from test_bin;
select hex(bin_id) as bin_id from test_bin where bin_id = unhex(‘fa34e10293cb42848573a4e39937f479‘);
select hex(bin_id) as bin_id from test_bin where bin_id = unhex(?);
select hex(bin_id) as bin_id from test_bin where bin_id = x‘fa34e10293cb42848573a4e39937f479‘;
查询结果:
bin_id
--------------------------
fa34e10293cb42848573a4e39937f479
备注:使用mysql内置的 uuid() 创建一个函数返回 binary(16)类型的uuid值
create function uu_id() returns binary(16) return unhex(replace(uuid(),‘-‘,‘‘));
或
create function uu_id() returns binary(16) return unhex(reverse(replace(uuid(),‘-‘,‘‘)));
使用:
insert into test_bin(bin_id) values(uu_id());
范例1:
connection conn = null;
statement stat = null;
resultset rs = null;
try {
conn = jdbcutils.getconnection(map);
string sql = "select hex(recid) as recid,stdname as stdname ,hex(resid) as resid from jyyt";
stat = conn.createstatement();
rs = stat.executequery(sql);
while (rs.next()) {
string recid = rs.getstring("recid");
string staname = rs.getstring("stdname");
string resid = rs.getstring("resid");
system.out.println(recid + "---" + staname + "---" + resid);
}
} catch (sqlexception e) {
e.printstacktrace();
} finally {
jdbcutils.closeconnection(conn, stat, rs);
}
范例2:
select
y.unitid as unitid,
y.unitname as unitname,
y.warningtype as warningtype
from
gxjt_yj as y
left join md_org as m on m.recid = y.unitid
left join pms_company_info as p on m.recid = p.unitid
where
hex(m.parents) like '%66f7b47c80000101d5e8abf15cd9da73%'
and y.warningtype = 'registration_different'
未使用hex()函数:
使用hex()函数:
补充知识:【mysql】如何使用navicat查看mysql数据库中varbinary变量内容?
环境
navicat软件版本:navicat premium 11.1.13(64-bit)
mysql数据库版本:5.7
问题的提出
如题。
步骤
解决方法很简单,备忘。
1、数据库表的设计如下,表中photo变量类型为varbinary。
2、navicat软件显示为乱码,如下。
3、右键“保存数据为”,保存为1.txt。文件名随便起。
4、用ue打开,显示如下。
以上这篇mysql中的binary类型使用操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持萬仟网。
希望与广大网友互动??
点此进行留言吧!