【发布时间】:2012-01-01 20:04:38
【问题描述】:
我经营一个购物车,其中的产品可以有更多选择。例如,product1 可以有多个选项,例如:
尺寸: s,m,l,xl
颜色: 红、蓝、绿
默认产品存储在customers_basket。
额外选择的项目存储在customers_basket_attributes。
在这两个表中,我插入了一个名为 products_unique_id 的唯一键。
这是为了识别客户何时添加具有相同选项的其他产品(如果是)
我只需要更新customers_basket 表的数量字段。
如果用户插入相同的产品但具有不同的选项,products_unique_id 会更改。
以下查询不起作用:
SELECT * FROM
customers_basket_attributes cba,
customers_basket cb
WHERE
cba.products_id = cb.products_id
AND
cba.products_unique_id = cb.products_unique_id
AND customers_id="1"
但是,此查询有效:
SELECT * FROM
customers_basket cb,
products p,
products_description pd
WHERE
cb.products_id = p.products_id
AND
cb.products_id = pd.products_id
AND customers_id="1"
这也有效:
SELECT * FROM
customers_basket_attributes cba,
products p,
products_description pd
WHERE
cba.products_id = p.products_id
AND
cba.products_id = pd.products_id
AND customers_id="1"
数据库结构和记录
以下是测试上述查询所需的数据库结构和记录:
DROP TABLE IF EXISTS customers_basket;
CREATE TABLE customers_basket (
customers_basket_id INTEGER PRIMARY KEY ASC,
customers_id int NOT NULL,
products_id int NOT NULL,
products_unique_id tinytext NOT NULL,
customers_basket_quantity int(2) NOT NULL,
final_price decimal(15,4),
customers_basket_date_added datetime
);
DROP TABLE IF EXISTS customers_basket_attributes;
CREATE TABLE customers_basket_attributes (
customers_basket_attributes_id INTEGER PRIMARY KEY ASC,
customers_id int NOT NULL,
products_id int NOT NULL,
products_unique_id tinytext NOT NULL,
products_options_id int NOT NULL,
products_options_txt varchar(64) NOT NULL default '',
products_options_value_id int NOT NULL,
products_options_value_txt varchar(64) NOT NULL default ''
);
此外,我还包括了下表,即使测试问题查询不需要它们。
DROP TABLE IF EXISTS products;
CREATE TABLE products (
products_id int NOT NULL,
products_quantity int(4) NOT NULL,
products_model varchar(12),
products_image varchar(64),
products_price decimal(15,4) NOT NULL,
products_date_added datetime NOT NULL,
products_last_modified datetime,
products_date_available datetime,
products_weight decimal(5,2) NOT NULL,
products_status tinyint(1) NOT NULL,
products_tax_class_id int NOT NULL,
manufacturers_id int NULL,
products_ordered int NOT NULL default '0',
PRIMARY KEY (products_id)
);
DROP TABLE IF EXISTS products_description;
CREATE TABLE products_description (
products_id int NOT NULL,
language_id int NOT NULL default '1',
products_name varchar(64) NOT NULL default '',
products_description text,
products_url varchar(255) default NULL,
products_viewed int(5) default '0',
PRIMARY KEY (products_id,language_id)
);
INSERT INTO customers_basket_attributes VALUES (1,1,1,1{4}1{3}5{5}10,4,Memory,1,4 mb);
INSERT INTO customers_basket_attributes VALUES (2,1,1,1{4}1{3}5{5}10,3,Model,5,Value);
INSERT INTO customers_basket_attributes VALUES (3,1,1,1{4}1{3}5{5}10,5,Version,10,Download: Windows - English);
INSERT INTO customers_basket_attributes VALUES (4,1,2,2{4}3{3}6,4,Memory,3,16 mb);
INSERT INTO customers_basket_attributes VALUES (5,1,2,2{4}3{3}6,3,Model,6,Premium);
INSERT INTO customers_basket VALUES(1,1,1,1{4}1{3}5{5}10,1,0,DATETIME('NOW'));
INSERT INTO customers_basket VALUES(2,1,2,2{4}3{3}6,2,0,DATETIME('NOW'));
INSERT INTO products VALUES (1,32,'MG200MMS','matrox/mg200mms.gif',299.99, DATETIME('NOW'),null,null,23.00,1,1,1,0);
INSERT INTO products VALUES (2,32,'MG400-32MB','matrox/mg400-32mb.gif',499.99, DATETIME('NOW'),null,null,23.00,1,1,1,0);
INSERT INTO `products_description` VALUES(1, 1, 'Matrox G200 MMS', '', 'www.matrox.com/mga/products/g200_mms/home.cfm', 0);
INSERT INTO `products_description` VALUES(2, 1, 'Matrox G400 32MB', '', 'www.matrox.com/mga/products/mill_g400/home.htm', 0);
更新:我刚刚发现它在表 customers_basket_attributes 和 customers_basket 之间失败了。
表 products 和 products_description 工作正常,如果将它们与其他 2 个表单独检查。
** 由于原则,我认为@Jonathan Leffler 应该得到赏金。不仅是为了他的回答,也是为了他努力让我理解他给出的答案。所以请不要试图赚取这个赏金。如果我一直被阻止,我想对我的最后一个行动充满信心**
【问题讨论】:
-
你为什么不先解释一下你想要完成什么?也可能是架构的相关部分。
-
我不同意你的观点,很难说你想做什么。无论如何,祝你好运。
-
建议您发布示例数据和所需结果。
-
这就像你在 Stack Overflow 上回答了所有最糟糕的问题,在这些问题上编译了我所有的 cmets,然后做了与我要求那些原始海报做的完全相反的事情,并形成了这个问题作为结果.
-
@whitehat:仅仅因为you don't think you have a need for the question and answer anymore 并不意味着您应该清除所有内容并使所有答案无效。我们在 Stack Overflow 上认为这非常很糟糕,社区倾向于标记这种行为,这可能会促使管理员采取进一步行动。