本文主要向大家介紹了MySQL數(shù)據(jù)庫之MySQL的binary類型操作,通過具體的內(nèi)容向大家展現(xiàn),希望對大家學(xué)習(xí)MySQL數(shù)據(jù)庫有所幫助。
示例數(shù)據(jù)表:
CREATE TABLE test_bin (
bin_id BINARY(16) NOT NULL
) Engine=InnoDB;
插入數(shù)據(jù)(內(nèi)容是一個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‘);
查詢數(shù)據(jù):
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‘;
查詢結(jié)果:
bin_id
--------------------------
FA34E10293CB42848573A4E39937F479
備注:使用MySQL內(nèi)置的 UUID() 創(chuàng)建一個函數(shù)返回 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()函數(shù):
使用HEX()函數(shù):
補(bǔ)充知識:【MySQL】如何使用Navicat查看mysql數(shù)據(jù)庫中varbinary變量內(nèi)容?
環(huán)境
Navicat軟件版本:Navicat premium 11.1.13(64-bit)
mysql數(shù)據(jù)庫版本:5.7
問題的提出
如題。
步驟
解決方法很簡單,備忘。
1、數(shù)據(jù)庫表的設(shè)計(jì)如下,表中photo變量類型為varbinary。
2、Navicat軟件顯示為亂碼,如下。
3、右鍵“保存數(shù)據(jù)為”,保存為1.txt。文件名隨便起。
4、用UE打開,顯示如下。
以上這篇MySQL中的binary類型使用操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- MYSQL配置參數(shù)優(yōu)化詳解
- MySql數(shù)據(jù)庫備份的幾種方式
- Mysql查詢語句優(yōu)化技巧