c# - 对 MySQL 表进行排序,然后更新行

标签 c# mysql

我想对表进行排序 http://sqlfiddle.com/#!2/87fa6按 jobs.datetime 升序排列,然后仅选择 jobs.operator_id=0jobs.status=open 的行。

一旦表具有此结构,我想设置 jobs.operator_id=bidList[i].OperatorId 但这应该只发生在 booking.plot_id=bid.plot_id< 的行中.

我已经购买了以下 2 个 SQL 语句 - 是否可以以某种方式将它们组合起来以满足上述要求?

// Allocate jobs to bids
for (var i = 0; i < bidList.Count; i++)
{

    // Select rows where booking.status=open, booking.postcode=_plot and
    // booking.operator_id=0. 
    // Once this is done, order by datetime

    query = "SELECT operator_id, plot_id, status FROM booking " +
            "WHERE status='open' AND postcode='" + _plot + "' AND operator_id=0" +
            "ORDER BY datetime";

    // Whilst maintaining the structure above, set 
    // booking.operator_id=bidList[i].OperatorId, 
    // booking.status=allocated where BidList.PlotId = jobs.plotid

    query += "UPDATE t1 SET t1.operator_id='" +
             bidList[i].OperatorId + "', t1.status='Allocated' " +
             "FROM booking_view T1 "+ "JOIN bid t2 ON t1.plot_id=t2.plot_id";
}           

最佳答案

像这样更改第二个查询:

"UPDATE t1 SET t1.operator_id='" +
bidList[i].OperatorId + "', t1.status='Allocated' " +
"FROM booking_view T1 "+
"JOIN bid t2 ON t1.plot_id=t2.plot_id";

关于c# - 对 MySQL 表进行排序,然后更新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28202784/

相关文章:

mysql - 计算并连接两个表

java - Android - 将参数发送到 php 脚本以在查询中使用它

php - 获取计数并显示要显示的订单号

c# - 如何在 iOS mono 中禁用后退按钮

c# - 为什么 MVVM-Light 中的 ObservableObject 不再实现 INotifyPropertyChanged?

c# - HTML.TextBoxFor 禁用不传递值

c# - WPF ViewModel在另一个线程中运行方法,报告进度,更改Button的IsEnabled并显示结果

python - 连接到 python 中的数据库 url,其中 url 用于 jdbc

c# - 获取当前打开的窗口名称

MySQL存储与优化