gpt4 book ai didi

sqlite - 连接表的SQLite错误

转载 作者:行者123 更新时间:2023-12-03 19:46:36 25 4
gpt4 key购买 nike

我有以下表格:

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
);


因此,在表中具有以下数据:

在“产品”表中

---------------------
1 | roll | 100
2 | cheese | 500
3 | burger | 300
4 | mega burger | 550


在“订单”表中

-------------
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中不起作用?

最佳答案

在连接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

关于sqlite - 连接表的SQLite错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20780442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com