sqlite连接问题
以下是我的数据库模式:sqlite连接问题,sqlite,Sqlite,以下是我的数据库模式: CREATE TABLE orders ( transaction_id integer primary key autoincrement, total_price integer not null ); CREATE TABLE order_items ( transaction_id integer REFERENCES orders(transaction_id), SKU integer not null, product
CREATE TABLE orders (
transaction_id integer primary key autoincrement,
total_price integer not null
);
CREATE TABLE order_items (
transaction_id integer REFERENCES orders(transaction_id),
SKU integer not null,
product_name text not null,
unit_price integer not null,
quantity integer not null
);
我试图做的是从orders表中获取最后一行,并将该事务\u id与orders\u items表中包含相同事务\u id的所有项目相匹配
我尝试了这个sqlite查询,但没有结果:
sqlite> SELECT orders.transaction_id, orders.total_price
...> FROM orders
...> ORDER BY transaction_id DESC LIMIT 1
...> WHERE orders.transaction_id = order_items.transaction_id;
订单必须在WHERE后面。 但是,限制1将只返回一条记录 如果不在from子句中提及,则无法从
order\u items
访问列
您应该使用子查询筛选事务:
SELECT *
FROM orders
JOIN order_items USING (transaction_id)
WHERE orders.transaction_id = (SELECT MAX(transaction_id)
FROM orders)