简体   繁体   English

高级SQL查询帮助。 需要将Ruby脚本转换为SQL查询

[英]advanced SQL query help. need to translate ruby script to SQL query

I am pretty new with SQL and i have to translate this ruby script to SQL because the ruby script is taking WAY too long to run on the server. 我对SQL很陌生,我不得不将此ruby脚本转换为SQL,因为ruby脚本花费的时间太长,无法在服务器上运行。 Here is the ruby script: 这是ruby脚本:

orders = Order.find_all_by_state("in_progress"); nil
orders.each do |o|
  if o.line_items.length == 0 and o.created_at > Time.parse("2011-12-14 00:00:00.000000 -08:00") and o.created_at < Time.parse("2011-12-21 00:00:00.000000 -08:00")
    o.adjustments[0].destroy
    o.shipment.destroy
    o.turnaround_time.destroy
    o.checkout.destroy
    o.destroy
  end; nil
end; nil

Anyway, the associations are as follows: 无论如何,关联如下:

an order has one checkout, shipment, turnaround time, many line_items, and many adjustments (the orders i want to delete only have 1). 订单有一个结帐,装运,周转时间,许多line_items和许多调整(我要删除的订单只有1个)。 I want to delete all orders that have no line items and was created within those range of dates (dec 15th-20th). 我想删除所有没有订单项且在这些日期范围内(12月15日至20日)创建的订单。 I also want to delete all objects associated with those orders (shipments, turnaround_times, checkouts, adjustments). 我还想删除与这些订单关联的所有对象(装运,turnaround_times,结帐,调整)。 I am new to SQL and i have absolutely no idea how to do this. 我是SQL新手,我绝对不知道如何执行此操作。 I've been looking at SQL documentation but i am really struggling with the syntax for this. 我一直在看SQL文档,但是我确实在为此语法苦苦挣扎。 If i could get some help, that would be great. 如果我能得到一些帮助,那就太好了。

To delete orders without any line items: 要删除不包含任何订单项的订单:

delete from order
where order_id not in (select order_id from line_item);

To delete associated objects that (now) don't have an order: 要删除(现在)没有订单的关联对象:

delete from shipment
where order_id not in (select order_id from order);

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

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