SQL选择查询问题

编程入门 行业动态 更新时间:2024-10-09 22:15:12
本文介绍了SQL选择查询问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一张带有ID(自动编号),dealerMobile,totalCost和deliveryDate的表格。 一些示例数据:

ID,dealerMobile,totalCost,deliveryDate 1,01723325484, 5000,20-Apr-14 2,01723325484,1000,22-Apr-14 3,01723325484,2000,24-Apr-14 4,01852136544,7000,24-Apr -14

我需要根据最后插入的dealerMobile显示一行。那就是如果我搜索01723325484那么它只显示第三行。目前我正在应用以下查询,它不能正常工作。

SELECT DISTINCT dealerMobile,totalCost,deliveryDate FROM tbMain WHERE dealerMobile = ' + txtdealerMobile.Text +' ORDER BY deliveryDate DESC

请帮忙。

解决方案

你想在SQL中使用find-Nth-pattern

选择 dealerMobile,totalCost,deliveryDate FROM ( 选择 ROW_NUMBER() over ( partition by dealerMobile,totalCost, convert ( date ,deliveryDate )订单 按 dealerMobile,totalCost,转换 ( date ,deliveryDate)) as rownr, dealerMobile,totalCost,deliveryDate 来自 tbMain 其中 dealerMobile = ' 在此设置所需数字' - < - 您在这里的输入 订单 按 deliverydate desc ) as rows where 行.rownr = 1

更原始的ansi sql方法来做同样的事情

SELECT dealerMobile,totalCost,deliveryDate FROM tbMain 其中 dealerMobile = ' 01723325484' 和 deliveryDate =(选择 max(deliveryDate)来自 tblMain 其中 dealerMobile = ' 01723325484' )

I have a table with ID(auto number),dealerMobile, totalCost and deliveryDate. Some example data:

ID, dealerMobile, totalCost, deliveryDate 1, 01723325484, 5000, 20-Apr-14 2, 01723325484, 1000, 22-Apr-14 3, 01723325484, 2000, 22-Apr-14 4, 01852136544, 7000, 23-Apr-14

I need to Show only one row based on dealerMobile which is lastly insert. That is If I Search 01723325484 then It show me the third row only. Currently I am applying the below query, It's not working as I want.

SELECT DISTINCT dealerMobile, totalCost, deliveryDate FROM tbMain WHERE dealerMobile = '" + txtdealerMobile.Text + "' ORDER BY deliveryDate DESC

Please help.

解决方案

You want to use the find-Nth-pattern in SQL

Select dealerMobile, totalCost, deliveryDate FROM ( select ROW_NUMBER() over (partition by dealerMobile, totalCost, convert(date, deliveryDate) order by dealerMobile, totalCost, convert(date, deliveryDate)) as rownr, dealerMobile, totalCost, deliveryDate from tbMain where dealerMobile='set the wanted number here' -- <-- your input here order by deliverydate desc ) as rows where rows.rownr = 1

a more primitive ansi sql way to do the same

SELECT dealerMobile, totalCost, deliveryDate FROM tbMain where dealerMobile = '01723325484' and deliveryDate = (Select max(deliveryDate) from tblMain where dealerMobile = '01723325484')

更多推荐

SQL选择查询问题

本文发布于:2023-11-25 11:55:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629642.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!