确定 SQL 按 ASC 或 DSC 排序

发布于 2024-08-04 20:29:36 字数 780 浏览 5 评论 0原文

我有一个从 MySQL 数据库接收值的数据列表。该数据列表可以按各种列值(例如“标题”、“作者”、“发布日期”等)排序。要确定按什么值排序,将该值插入到查询字符串中。

即 www.web.com/default.aspx?order_by=title

我还确定是否按升序或降序排序。

即 www.web.com/default.aspx?order_by=title&direction=asc

我希望能够将方向设置为 asc 如果

(我想要做的工作示例可以在

http://www.milwaukeejobs.com/jobs/category/Information-Technology-Internet-Web-Development/Milwaukee,-WI/1306

*如果您单击排序,请注意值,例如按日期,它设置为 ASC,如果您再次单击它,它设置为 DSC;我想在我的网站上执行此操作 * )

我不知道如何执行此操作。我正在考虑 If Page.IsPostback 条件,但单击 url 不会导致回发。我考虑过获取发送者对象或通过 Page.Onload 事件发送的 system.eventargs 变量,但我不确定是否可以用这些做任何事情。有什么想法吗?

I have a datalist that receives values from a MySQL database. This datalist can be sorted by various column values such 'Title', 'Author', 'Published Date', etc. To determine what value to sort by, the value is inserted into the query string.

I.e. www.web.com/default.aspx?order_by=title

I also determine wheter to order in ascending order or descending order.

I.e. www.web.com/default.aspx?order_by=title&direction=asc

I want to be able to set the direction to asc if the

(a working example of what I'm looking to do can be found at

http://www.milwaukeejobs.com/jobs/category/Information-Technology-Internet-Web-Development/Milwaukee,-WI/1306

*Notice if you click the sort values such as by Date its set to ASC, if you click it again its set to DSC; I'd like to do this on my site * )

I'm not sure how to do this. I was thinking of a If Page.IsPostback condition, but clicking the url does not cause a postback. I thought about grabbing the sender object, or system.eventargs variables that are sent through the Page.Onload event, but I'm not sure if I could do anything with these. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丿*梦醉红颜 2024-08-11 20:29:36

我会将页面本身中列的排序顺序保留为隐藏输入字段(指定 runat="server")。单击链接时,您可以在回发之前翻转该字段的方向。这可以避免检查 IsPostBack 状态。

类似这样:

<input type="hidden" id="TitleSortOrder" Runat="server" />
<asp:LinkButton id="TitleHeader" onclick="return(FlipSortOrder('Title'));" />

function FlipSortOrder(type)
{
  var sortOrder = document.getElementById(type + "SortOrder");
  sortOrder.value = sortOrder.value === "ASC" ? "DESC" : "ASC";
  return true;
}

在服务器端代码中,您可以使用 TitleSortOrder.Value 属性引用标题排序方向的值。

为了在 MySQL 中实现此功能,如果您打算在数据库级别进行排序,则需要执行动态查询。这类似于帖子 Dynamic Column name in MYSQL,但为了保持总而言之,在 MySQL 中执行动态 SQL 的模式如下所示:

SET @qry = 'your query here';
PREPARE stmt FROM @qry;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

I would maintain the sort order of the columns within the page itself as hidden input fields (with runat="server" specified). When a link is clicked, you can flip the direction of this field prior to postback. This gets around having to check for IsPostBack status.

Something like this:

<input type="hidden" id="TitleSortOrder" Runat="server" />
<asp:LinkButton id="TitleHeader" onclick="return(FlipSortOrder('Title'));" />

function FlipSortOrder(type)
{
  var sortOrder = document.getElementById(type + "SortOrder");
  sortOrder.value = sortOrder.value === "ASC" ? "DESC" : "ASC";
  return true;
}

In server-side code, you can refer to the value of the title sort direction using the TitleSortOrder.Value property.

To make this work in MySQL, you'll need to execute a dynamic query if you intend for the sorting to happen at the database level. This is similar to post Dynamic Column name in MYSQL, but in the interests of keeping this all together, the pattern of executing dynamic SQL in MySQL looks like this:

SET @qry = 'your query here';
PREPARE stmt FROM @qry;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文