簡體   English   中英

使用sqlite3中的嵌套查詢從多個表中選擇列

[英]select columns from multiple tables using nested query in sqlite3

我的SQLite3數據庫中有3個表- listsitemslist_items list_items是一個多對多表,由以下幾列組成: list_iditem_idquantity -其中list_id來自lists表, item_id來自items表,而quantity是一個新字段。 在我的應用程序中,我想顯示給定列表的項目名稱及其對應的數量。 我可以使用此SQL查詢獲取給定列表中的所有項目:

select item_name from items where item_id in (select item_id from list_items where list_id=1)

但是我不知道如何從list_items以及上面的查詢中的名稱中獲取列數。 有人可以幫我解決這個問題嗎?

您可以在下面嘗試-使用連接

select item_name,quantity
from items i inner join list_items li on i.item_id =li.item_id 
where list_id=1

為什么不JOIN

select i.item_name, li.quantity   
from items i inner join
     list_items li
     on li.item_id = i.item_id
where li.list_id = 1;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM