当前位置: 首页 > news >正文

erlang mnesia 数据库查询

Mnesia是一个分布式数据库管理系统,适合于电信和其它需要持续运行和具备软实时特性的Erlang应用,越来越受关注和使用,但是目前Mnesia资料却不多,很多都只有官方的用户指南。下面的内容将着重说明 如何做 Mnesia 数据库查询。

示例中表结构的定义:

%% 账号表结构 
-record( y_account,{ id, account, password }).

%% 资料表结构  
-record( y_info, { id, nickname, birthday, sex }). 

1、查询全部记录

%%===============================================
%%  select * from y_account
%%===============================================

%% 使用 mnesia:select
F = fun() ->
	MatchHead = #y_account{ _ = '_' },
	Guard = [],
	Result = ['$_'],
	mnesia:select(y_account, [{MatchHead, Guard, Result}])
end,
mnesia:transaction(F).

%% 使用 qlc
F = fun() ->
	Q = qlc:q([E || E <- mnesia:table(y_account)]),
	qlc:e(Q)
end,
mnesia:transaction(F).

2、查询部分字段的记录

%%===============================================
%%  select id,account from y_account
%%===============================================

%% 使用 mnesia:select
F = fun() ->
	MatchHead = #y_account{id = '$1', account = '$2', _ = '_' },
	Guard = [],
	Result = ['$$'],
	mnesia:select(y_account, [{MatchHead, Guard, Result}])
end,
mnesia:transaction(F).

%% 使用 qlc
F = fun() ->
	Q = qlc:q([[E#y_account.id, E#y_account.account] || E <- mnesia:table(y_account)]),
	qlc:e(Q)
end,
mnesia:transaction(F).

3、Where 查询

%%===============================================
%%    select account from y_account where id>5
%%===============================================

%% 使用 mnesia:select
F = fun() ->
	MatchHead = #y_account{id = '$1', account = '$2', _ = '_' },
	Guard = [{'>', '$1', 5}],
	Result = ['$2'],
	mnesia:select(y_account, [{MatchHead, Guard, Result}])
end,
mnesia:transaction(F).

%% 使用 qlc
F = fun() ->
	Q = qlc:q([E#y_account.account || E <- mnesia:table(y_account), E#y_account.id>5]),
	qlc:e(Q)
end,
mnesia:transaction(F).

如果查找主键 key=X 的记录,还可以这样子查询:

%%===============================================
%%   select * from y_account where id=5
%%===============================================

F = fun() ->
	mnesia:read({y_account,5})
end,
mnesia:transaction(F).

如果查找非主键 field=X 的记录,可以如下查询:

%%===============================================
%%   select * from y_account where account='xiaomin'
%%===============================================

F = fun() ->
	MatchHead = #y_account{ id = '_', account = "xiaomin", password = '_' },
	Guard = [],
	Result = ['$_'],
	mnesia:select(y_account, [{MatchHead, Guard, Result}])
end,
mnesia:transaction(F).

4、Order By 查询

%%===============================================
%%   select * from y_account order by id asc
%%===============================================

%% 使用 qlc
F = fun() ->
	Q = qlc:q([E || E <- mnesia:table(y_account)]),
	qlc:e(qlc:keysort(2, Q, [{order, ascending}]))
end,
mnesia:transaction(F).

%% 使用 qlc 的第二种写法
F = fun() ->  
	Q = qlc:q([E || E <- mnesia:table(y_account)]), 
	Order = fun(A, B) ->
		B#y_account.id > A#y_account.id
	end,
	qlc:e(qlc:sort(Q, [{order, Order}]))
end,  
mnesia:transaction(F).

5、Join 关联表查询

%%===============================================
%%   select y_info.* from y_account join y_info on (y_account.id = y_info.id)
%%      where y_account.account = 'xiaomin'
%%===============================================

%% 使用 qlc
F = fun() ->
	Q = qlc:q([Y || X <- mnesia:table(y_account),
		X#y_account.account =:= "xiaomin",
		Y <- mnesia:table(y_info),
		X#y_account.id =:= Y#y_info.id
	]),
	qlc:e(Q)
end,
mnesia:transaction(F).

6、Limit 查询

%%===============================================
%%   select * from y_account limit 2
%%===============================================

%% 使用 qlc
F = fun() ->
	Q = qlc:q([E || E <- mnesia:table(y_account)]),
	QC = qlc:cursor(Q),
	qlc:next_answers(QC, 2)
end,
mnesia:transaction(F).


注:使用qlc模块查询,需要在文件顶部声明“-include_lib("stdlib/include/qlc.hrl").”,否则编译时会产生“Warning: qlc:q/1 called, but "qlc.hrl" not included”的警告。

相关文章:

  • HDU 3264 Open-air shopping malls (计算几何-圆相交面积)
  • 2014Microsoft 校招笔试真题(找工作的虾米们赶紧做题晒答案喽)
  • 黑马程序员_IO流基本操作(Writer,Reader)
  • aptana 插件离线下载方式
  • Eclipse安装aptana 插件的方法
  • VC写的双人版俄罗斯方块
  • 2014百度校招笔试题之动态链接库静态链接库详解
  • centos 安装与操作
  • win7 防火墙开启ping
  • 人人网2014笔试算法题汇总
  • 暴风影音2014笔试算法题汇总
  • 华为2014笔试算法题汇总
  • 百度2014笔试算法题汇总
  • 美团网2014笔试算法题汇总
  • 2014百度校招笔试题
  • -------------------- 第二讲-------- 第一节------在此给出链表的基本操作
  • extjs4学习之配置
  • IDEA 插件开发入门教程
  • js中的正则表达式入门
  • Otto开发初探——微服务依赖管理新利器
  • php的插入排序,通过双层for循环
  • PV统计优化设计
  • react 代码优化(一) ——事件处理
  • spring boot下thymeleaf全局静态变量配置
  • use Google search engine
  • 前端_面试
  • 想使用 MongoDB ,你应该了解这8个方面!
  • 一个普通的 5 年iOS开发者的自我总结,以及5年开发经历和感想!
  • 用quicker-worker.js轻松跑一个大数据遍历
  • 你对linux中grep命令知道多少?
  • 【云吞铺子】性能抖动剖析(二)
  • ###项目技术发展史
  • (附源码)spring boot北京冬奥会志愿者报名系统 毕业设计 150947
  • (附源码)spring boot车辆管理系统 毕业设计 031034
  • (附源码)springboot社区居家养老互助服务管理平台 毕业设计 062027
  • (论文阅读30/100)Convolutional Pose Machines
  • (算法)求1到1亿间的质数或素数
  • (算法二)滑动窗口
  • (转)平衡树
  • (转)一些感悟
  • .NET 设计模式—简单工厂(Simple Factory Pattern)
  • .NET/C# 使用 #if 和 Conditional 特性来按条件编译代码的不同原理和适用场景
  • .NET企业级应用架构设计系列之应用服务器
  • .Net转Java自学之路—SpringMVC框架篇六(异常处理)
  • [ HTML + CSS + Javascript ] 复盘尝试制作 2048 小游戏时遇到的问题
  • [AIGC] Kong:一个强大的 API 网关和服务平台
  • [Angularjs]asp.net mvc+angularjs+web api单页应用之CRUD操作
  • [ASP.NET MVC]如何定制Numeric属性/字段验证消息
  • [BJDCTF 2020]easy_md5
  • [C++][基础]1_变量、常量和基本类型
  • [HDU 3555] Bomb [数位DP]
  • [I2C]I2C通信协议详解(二) --- I2C时序及规格指引
  • [iphone-cocos2d]关于Loading的若干处理和讨论
  • [LeetCode]Max Points on a Line
  • [linux c]linux do_div() 函数用法