简体   繁体   English

排序MySQL表,然后更新行

[英]Sorting MySQL table and then updating the rows

I want to sort the table at http://sqlfiddle.com/#!2/87fa6 by jobs.datetime ascending and then select only rows where jobs.operator_id=0 and jobs.status=open . 我想按Jobs.datetime升序对http://sqlfiddle.com/#!2/87fa6上的表进行排序,然后仅选择jobs.operator_id=0jobs.status=open

Once the table has this structure, I want to set jobs.operator_id=bidList[i].OperatorId but this should only happen in rows where booking.plot_id=bid.plot_id . 表具有此结构后,我要设置jobs.operator_id=bidList[i].OperatorId但这仅应在booking.plot_id=bid.plot_id行中booking.plot_id=bid.plot_id

I've bought it down to the following 2 SQL statements - would be possible to combine them somehow so they meet the requirement above? 我已将其购买到以下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";
}           

Change your second query like this: 像这样更改第二个查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM