我有以下表格:
PRAGMA foreign_keys = ON;
CREATE TABLE products(
'_id' INTEGER PRIMARY KEY AUTOINCREMENT,
'name' VARCHAR(20),
'price' INTEGER
);
CREATE TABLE orders(
'_id' INTEGER PRIMARY KEY AUTOINCREMENT,
'order_number' INTEGER,
'order_sum' INTEGER
);
CREATE TABLE ord_details(
'_id' INTEGER PRIMARY KEY AUTOINCREMENT,
'order_id' INTEGER,
'product_id' INTEGER,
'product_count' INTEGER,
FOREIGN KEY (order_id) REFERENCES orders(_id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (product_id) REFERENCES products(_id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE ord_times(
'_id' INTEGER PRIMARY KEY AUTOINCREMENT,
'order_id' INTEGER,
'start_t' TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
'end_t' TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (order_id) REFERENCES orders(_id) ON DELETE CASCADE ON UPDATE CASCADE
);
因此,在表格中有以下数据:
在'products'表
中---------------------
1 | roll | 100
2 | cheese | 500
3 | burger | 300
4 | mega burger | 550
在'orders'表
中-------------
1 | 11 | 600
在'ord_details'表
中--------------
1 | 1 | 1 | 1
1 | 1 | 2 | 1
在'ord_times'表
中--------------------------------------------------------
1 | 1 | 12/23/2013 12:24:38 PM | 12/23/2013 12:27:02 PM
当我执行此SQL命令时,它给出的结果不正确:
SELECT orders.order_number, products.name as prname, products.price, ord_details.product_count, orders.order_sum, ord_times.start_t, ord_times.end_t FROM orders
LEFT JOIN ord_details ON orders._id=ord_details.order_id
LEFT JOIN products ON ord_details.product_id=products._id
LEFT JOIN ord_times ON orders._id=ord_times.order_id
结果:
-----------------------------------------------------------------------------------
11 | roll | 100 | 1 | 600 | 12/23/2013 12:24:38 PM | 12/23/2013 12:27:02 PM
11 | cheese | 500 | 1 | 600 | 12/23/2013 12:24:38 PM | 12/23/2013 12:27:02 PM
11 | burger | 300 | 0 | 600 | 12/23/2013 12:24:38 PM | 12/23/2013 12:27:02 PM
11 | mega burger | 550 | 0 | 600 | 12/23/2013 12:24:38 PM | 12/23/2013 12:27:02 PM
我期待以下结果:
-----------------------------------------------------------------------------------
11 | roll | 100 | 1 | 600 | 12/23/2013 12:24:38 PM | 12/23/2013 12:27:02 PM
11 | cheese | 500 | 1 | 600 | 12/23/2013 12:24:38 PM | 12/23/2013 12:27:02 PM
当我在sqlite-shell-win32-x86-3080200中执行此代码时正常工作:
但在android中执行此SQL会返回错误的结果。 这里插入代码:
INSERT INTO products (name, price) VALUES ("roll", 100);
INSERT INTO products (name, price) VALUES ("cheese", 500);
INSERT INTO products (name, price) VALUES ("burger", 300);
INSERT INTO products (name, price) VALUES ("mega burger", 550);
INSERT INTO orders (order_number, order_sum) VALUES (11, 600);
INSERT INTO ord_details (order_id, product_id, product_count) VALUES (1, 1, 1);
INSERT INTO ord_details (order_id, product_id, product_count) VALUES (1, 2, 1);
INSERT INTO ord_times (order_id, start_t, end_t) VALUES (1,'12/23/2013 12:24:38 PM', '12/23/2013 12:27:02 PM');
为什么“ord_details.product_id = products._id”表达式在android中不起作用?
答案 0 :(得分:1)
在加入OrderDetails和Products表时尝试使用JOIN而不是LEFT JOIN,因此代码看起来像
SELECT orders.order_number, products.name as prname, products.price,
ord_details.product_count, orders.order_sum, ord_times.start_t, ord_times.end_t FROM orders
LEFT JOIN ord_details ON orders._id=ord_details.order_id
JOIN products ON ord_details.product_id=products._id
LEFT JOIN ord_times ON orders._id=ord_times.order_id