我有电子商务网站,我正在制作折扣优惠券系统。要申请折扣,我需要检查优惠券表中的一些验证,如下所示。我的问题是,如果查询发生在item_name,我无法找到行。
我的查询就像这样
SELECT * FROM `coupons` WHERE expiry_date > CURDATE() and (item_name like '%FLIP-2 Bluetooth Speaker, Credit Card Power bank (2500mAh)%') and code = 'Test300' and type = 'Product' and minimum_spend <= 6769 and status = 'active'
优惠券表
CREATE TABLE `coupons` (
`ID` int(11) NOT NULL,
`type` varchar(100) NOT NULL,
`name` varchar(100) NOT NULL,
`code` varchar(20) NOT NULL,
`item_name` varchar(5000) NOT NULL,
`discount` int(10) NOT NULL,
`valid_for` varchar(1000) NOT NULL,
`valid_from` varchar(60) NOT NULL,
`expiry_date` date NOT NULL,
`usageCount` varchar(10) NOT NULL,
`minimum_spend` int(10) NOT NULL,
`category` varchar(100) NOT NULL,
`description` varchar(1000) NOT NULL,
`status` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `coupons` (`ID`, `type`, `name`, `code`, `item_name`, `discount`, `valid_for`, `valid_from`, `expiry_date`, `usageCount`, `minimum_spend`, `category`, `description`, `status`) VALUES
(8, 'Product', 'Test', 'Test300', 'Round Neck - T Shirt (Blue), Round Neck - T Shirt (White), Credit Card Power bank (2500mAh), ', 300, '', '', '2016-12-12', '', 900, '', '', 'active');
用户在购物车页面上应用优惠券代码,该优惠券代码会检查以下条件
foreach (ListViewDataItem row in cartItemsList.Items) {
productName.Text = (row.FindControl("itemName") as Label).Text;
items.Text += ", " + productName.Text;
}
string query = "SELECT * FROM `coupons` WHERE expiry_date > CURDATE() and (item_name like @itemName) and code = @code and type = @type and minimum_spend >= @minimum_spend and status = @status";
string conString = ConfigurationManager.ConnectionStrings("conio2").ConnectionString;
using (MySqlConnection con = new MySqlConnection(conString)) {
using (MySqlCommand cmd = new MySqlCommand(query)) {
using (MySqlDataAdapter sda = new MySqlDataAdapter()) {
cmd.Parameters.AddWithValue("@itemName", "%" + items.Text + "%");
cmd.Parameters.AddWithValue("@code", promocode.Text);
cmd.Parameters.AddWithValue("@type", "Product");
cmd.Parameters.AddWithValue("@minimum_spend", subtotal.Text);
cmd.Parameters.AddWithValue("@status", "active");
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable()) {
sda.Fill(dt);
if (dt.Rows.Count > 0) {
discount = dt.Rows(0)("discount").ToString;
}
}
}
}
}
更新
这里我尝试连接item_name
的字符串Label productName = new Label();
Label items = new Label();
string discountCode = string.Empty;
int minimumSpend = 0;
int discount = 0;
string whereClause = string.Empty;
string joiner = string.Empty;
string itemCondition = string.Empty;
joiner = "";
foreach (ListViewDataItem row in cartItemsList.Items) {
productName.Text = (row.FindControl("itemName") as Label).Text;
items.Text += ", " + productName.Text;
}
for (i = 0; i <= cartItemsList.Items.Count - 1; i++) {
if (cartItemsList.Items.ToString.Contains(items.Text)) {
string cartItems = cartItemsList.Items(i).ToString;
itemCondition = string.Concat(itemCondition, joiner, string.Format("{0}", cartItems));
if (string.IsNullOrEmpty(joiner))
joiner = ",";
}
}
if (!string.IsNullOrEmpty(itemCondition)) {
whereClause = string.Concat(whereClause, joiner, string.Format("item_name Like '%{0}%'", itemCondition.Replace(",", "%' or item_Name Like '%")));
joiner = " and ";
}
string query = "SELECT * FROM `coupons` WHERE expiry_date > CURDATE() and " + whereClause + " code = @code and type = @type and minimum_spend >= @minimum_spend and status = @status";
string conString = ConfigurationManager.ConnectionStrings("conio2").ConnectionString;
using (MySqlConnection con = new MySqlConnection(conString)) {
using (MySqlCommand cmd = new MySqlCommand(query)) {
using (MySqlDataAdapter sda = new MySqlDataAdapter()) {
cmd.Parameters.AddWithValue("@itemName", "%" + items.Text + "%");
cmd.Parameters.AddWithValue("@code", promocode.Text);
cmd.Parameters.AddWithValue("@type", "Product");
cmd.Parameters.AddWithValue("@minimum_spend", subtotal.Text);
cmd.Parameters.AddWithValue("@status", "active");
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable()) {
sda.Fill(dt);
if (dt.Rows.Count > 0) {
discount = dt.Rows(0)("discount").ToString;
}
}
}
}
}