Sqlite में शामिल होने के साथ समस्या

यहाँ मेरा db स्कीमा है:

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

मैं जो करने की कोशिश कर रहा हूं वह ऑर्डर टेबल से अंतिम पंक्ति ले रहा है और उस लेन-देन से मेल खाता है।

मैंने बिना किसी लाभ के इस 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;

उत्तर:

उत्तर № 1 के लिए 1

आदेश कहां के बाद आना चाहिए। हालांकि, लिमिट 1 केवल एक रिकॉर्ड लौटाएगा।

आप कॉलम तक नहीं पहुंच सकते order_items FROM क्लॉज में इसका उल्लेख किए बिना।

आपको एक उपकुंजी के साथ लेनदेन को फ़िल्टर करना चाहिए:

SELECT *
FROM orders
JOIN order_items USING (transaction_id)
WHERE orders.transaction_id = (SELECT MAX(transaction_id)
FROM orders)