如何检查项目在特定日期范围内是否可用

How to Check if Item is available for certain Range of Date

我正在使用 SQL 数据库构建一个 C# Inventory 应用程序。使用此应用程序,您可以 select 预订一个项目,如果它可用,它将被预订。听起来很简单嗯!,这也是我的想法。这就是问题所在。 这个应用程序需要检查两个条件。

1) 如果商品在特定日期范围内可用(例如:8 月 18 日至 8 月 23 日)

2) 如果所需数量可用(例如:2 或 3)。

我有一个包含项目名称和初始数量的数据库。另一个保持电流 bookings.Which 有以下数据。

ID   Cottage    quantity   From Date    To Date     Item name
 2   Woodcastle    2       2016-08-18   2016-08-24   Kayaks

现在如果我 select 1 Kayak(初始数量 3)从 2016-08-19 到 2016-08-23 预订。我如何用 SQL 做到这一点?

这是我目前所做的,但运气不好

selecteditem = items_listbox.SelectedItem.ToString();
       from_date = from_datepicker.Value.ToString();
       to_date = to_datepicker.Value.ToString();
        quantity = quantity_need.Text;
        from_date = Convert.ToDateTime(from_date).ToString("yyyy-MM-dd");
        to_date = Convert.ToDateTime(to_date).ToString("yyyy-MM-dd");
        int init_i = 0;
        int taken_i = 0;
        int dropped_i = 0;
        string select_init = "SELECT initial_quantity from inventory_items Where item_name LIKE '" + selecteditem + "'; "+ "SELECT COALESCE(SUM(Quantity), 0) from inventory_bookings Where item_name LIKE '" + selecteditem + "' AND '" + from_date + "' between date_from and date_to; "+ "SELECT COALESCE(SUM(Quantity), 0) from inventory_bookings Where item_name LIKE '" + selecteditem + "' AND '" + to_date + "' between date_from and date_to";
        conn = new MySqlConnection( connectionstring);
        MySqlCommand init = new MySqlCommand(select_init, conn);
        try
        {
            conn.Open();
            reader = init.ExecuteReader();
            while (reader.Read())
            {
                init_i = reader.GetInt32(0);
            }
            reader.NextResult();
            while (reader.Read())
            {
               taken_i = reader.GetInt32(0);
            }
            reader.NextResult();
            while (reader.Read())
            {
                dropped_i = reader.GetInt32(0);
            }
            int quantity_avail= init_i - taken_i - dropped_i;
            if (quantity_avail >= Convert.ToInt32(quantity_need.Text))
            {
                checkresult_lbl.Text= "Currently There are "+ quantity_avail + " "+ selecteditem +"/s available in your Inventory, Please Proceed to book";
                Booking_btn.Enabled = true;
            }
           else
            {
                checkresult_lbl.Text = "Currently There are not enough available Items in your Inventory, Please change date or Quantity";
            }
         }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            conn.Close();
        }

我能够用这个逻辑解决这个问题。

Statement 1: "SELECT initial_quantity from inventory_items Where item_name LIKE '" + selecteditem + "'; "

Statement 2: "SELECT COALESCE(SUM(Quantity), 0) from inventory_bookings Where item_name LIKE '" + selecteditem + "' AND '" + from_date + "'<= date_to AND date_from < '"+to_date+"'"

语句#1 选择项目的初始值数量。 (比方说 3)。

语句 #2 选择所选日期的物品数量(比方说 2)

然后检查(3-2)是否大于或等于需要的数量,然后预订。