查询功能——“模糊查询”

查询功能——“模糊查询”

开发工具与关键技术:SQL Server 2014 Management Studio 模糊查询
作者:李国旭
撰写时间:2019年7月26日

在一些实际项目中——模糊查询这个功能往往能发挥很大的作用,而且模糊查询在我们手机、电脑上用来查找信息用得最多的一种搜索功能。那么它主要就是用于一些与“数据”相关的信息,通过它可快速查找到我们想要的信息;它通常是用关键字模糊查找,所谓关键字就是关区信息中所包含的字符等等。
所谓“模糊查询”,就是利用部分参数或者是字段查找到一些相关数据的方式。比如说按姓名查询拿到某条用户信息,仅指定姓名的某个部分,如姓或者名字中的某个字或者它们的组合,都可以找出与他们相关的一些信息数据。那么它的主要用来在我们已知信息少的情况下尽可能的找出我们所需要的数据信息。查询关区信息中的某条信息我们怎么做呢?首先,先获取它的查询条件:我呢是根据关区的编码来查询关区的信息,然后呢就重载表格的数据,给他条件Where这是页面上的。
下面我们一起来看一下这个例子:
在这里插入图片描述
控制器那边的就是要查询这个关区的所有的信息,多表的就要联表查询其他的表里面的信息(Join tb in mymodel+表名 on tb点那个表ID equals tb+ID)这样就可以联到其他的表和他的一些相关的信息和数据,如果想要倒序那就orderby+tb+主表的ID 最后加一个descending倒序,如下图所示:
在这里插入图片描述
查询的完整代码如下:

public ActionResult SelectDistrict(LayuiTablePage layuiTablePage, string ZoneCoding)
{
List<DistrictVo> listDistrict = (from tbDistrict in myModel.B_District
join tbCustoms in myModel.B_Custom on tbDistrict.CustomsID equals tbCustoms.CustomsID
join tbDoorPoint in myModel.B_DoorPoint on tbDistrict.DoorPointID equals tbDoorPoint.DoorPointID
orderby tbDistrict.DistrictID descending
select new DistrictVo{
ZoneCoding = tbDistrict.ZoneCoding,//关区编码
DistrictID = tbDistrict.DistrictID,//关区IDs
DoorPointID = tbDistrict.DoorPointID,//门点ID
CustomsID = tbDistrict.CustomsID,//海关ID
CustomsName = tbCustoms.CustomsName,//海关名称
ZoneEnglishName = tbDistrict.ZoneEnglishName,//英文名称
DoorPointName = tbDoorPoint.DoorPointName,//门点名称
Describe = tbDistrict.Describe,//描述
Location = tbDistrict.Location,//地址
ZoneInvalidation = tbDistrict.ZoneInvalidation,//是否启用
Website = tbDistrict.Website,//网址
ClosingTime1 = tbDistrict.ClosingTime.ToString(),//下班时间*/
Workday2 = tbDistrict.Workday.ToString(),//上班时间
Postcode = tbDistrict.Postcode,//邮编
Fax = tbDistrict.Fax,//传真
Email = tbDistrict.Email,//邮件
LXman = tbDistrict.LXman,//联系人
Telephone = tbDistrict.Telephone,//电话
MobilePhone = tbDistrict.MobilePhone//手机
}).ToList();
//模糊查询
if (!string.IsNullOrEmpty(ZoneCoding)){
listDistrict = listDistrict.Where(m => m.ZoneCoding.Contains(ZoneCoding)).ToList();
}
//计算数据总条数
int totalRow = listDistrict.Count();
//提取数据
List<DistrictVo> bdDistrict = listDistrict
.Skip(layuiTablePage.GetStartIndex())
.Take(layuiTablePage.limit)
.ToList();
//实例化
LayuiTableData<DistrictVo> layuiTableData = new LayuiTableData<DistrictVo>();
layuiTableData.count = totalRow;
layuiTableData.data = bdDistrict;
return Json(layuiTableData, JsonRequestBehavior.AllowGet);
}

下面的这句代码是最主要的模糊查询

//模糊查询
if (!string.IsNullOrEmpty(ZoneCoding)){
listDistrict = listDistrict.Where(m => m.ZoneCoding.Contains(ZoneCoding)).ToList();}

效果图如下图所示:
在这里插入图片描述
我是根据关区的编码来查询关区的信息的,下面我们可以看到有关于“J”的编码的关区信息查询得到的信息有两条,那么以上的就是我写的模糊查询。当然了我们也可以用模糊查询来查其他我们需要的信息。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Mybatis Plus中,实现模糊查询分页可以通过以下步骤来完成: 1. 在Mapper接口中定义查询方法:在BrandMapper接口中,定义了queryLikeByName方法,该方法接收name、current和size三个参数,并返回一个List<Brand>类型的结果。这个方法用于进行模糊查询,并指定查询结果的分页范围。 2. 在Service层实现方法:在BrandServiceImpl类中,重写了BrandService接口中的queryPage方法。在该方法中,首先从params参数中获取关键字key,然后根据传入的数据判断是否为空。如果key不为空,即表示需要进行模糊查询,调用BrandMapper中的queryLikeByName方法进行查询,并设置分页参数。如果key为空,则调用Mybatis Plus提供的page方法进行分页查询。 3. 返回结果:最后,将查询结果封装到PageUtils对象中,并返回给调用方。 综上所述,通过以上步骤,可以实现Mybatis Plus中的模糊查询分页功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Mybatis-Plus——分页+模糊查询](https://blog.csdn.net/weixin_52383177/article/details/127894664)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值