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

时间:2015-01-28 21:30:51

标签: c# mysql

我想通过jobs.datetime升序在http://sqlfiddle.com/#!2/87fa6对表格进行排序,然后只选择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";
}           

1 个答案:

答案 0 :(得分:1)

更改您的第二个查询:

"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";