我如何在我的插入语句中添加订单 [英] how do i add order by to my insert statement
本文介绍了我如何在我的插入语句中添加订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
am在班级中使用此代码将数据保存到我的数据库中.及其工作权利.
我现在希望将数据以订购单的形式输入到数据库中.
这就是按付款日期明细排序".如何将其添加到我的插入语句中,或如何使其按顺序保存到数据库中.谢谢
am using this codes in my class to save data into my db. and its working right.
i now want the data to be entered into the db in an order form.
that is "order by dateofpayment desc". how do i add this to my insert statement or how make it save into the db in an order. thanks
public void saveRegister5()
{
//saves the data in memory
SqlCommand cmd = new SqlCommand();
string dat = null;
dat = dateofpayment.Year + "-" + dateofpayment.Month + "-" + dateofpayment.Day;
string sqlQuery = null;
sqlQuery = "Insert into tblstaffsalarypaymentdetails values('" + staffid + "','" + purpose + "','" + months + "','" + years + "','" + dat + "','" + amountpaid + "','" + remarks + "')";
cmd.Connection = conn;
cmd.CommandText = sqlQuery;
cmd.CommandType = System.Data.CommandType.Text;
conn.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
}
推荐答案
正如其他人所说,INSERT并不真正负责排序,而SELECT(和索引)确实负责排序
*但是*-如果您创建聚集索引 [ ^ ]通过日期付款数据库,数据库将完成您所要的……但几乎绝对不是您真正想要的
固定链接
As the others have said, the INSERT is not really responsible for sorting, the SELECT (and indices) does
*However* - if you create a clustered index[^] on your DB by dateofpayment, the DB will do what you''re asking for ... but almost definitely not what you really want
edit: fixed link
数据库中数据的物理顺序没有任何意义.插入顺序无关紧要!
因此,只需插入然后使用带有 ORDER BY [
The physical order of data in a databse has no meaning. The order of insert is irrelevant!
So just insert and then use a query with an ORDER BY[^] statement to show the results in a specified order!
我同意Manas.使用Order By进行插入没有任何意义.但是,如果要从其他表中选择数据,则代码为
I Agree with Manas. There is no meaning of using Order By for insert. but, if you want to select data from onother table then here is the code-
INSERT INTO table
SELECT *
FROM (SELECT col1, col2, col3 FROM table1
UNION ALL
SELECT col1, col2, col3 FROM table2) AS my_union
ORDER BY col1, col2, col3
这篇关于我如何在我的插入语句中添加订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文