【问题标题】:select columns from multiple tables using nested query in sqlite3在 sqlite3 中使用嵌套查询从多个表中选择列
【发布时间】:2018-12-27 18:05:56
【问题描述】:

我的 SQLite3 数据库中有 3 个表 - listsitemslist_items。表list_items 是一个多对多表,包含以下列-list_iditem_idquantity-其中list_id 来自lists 表,item_id 来自itemsquantity 是一个新字段。在我的应用程序中,我想显示给定列表的项目名称及其相应数量。我可以使用这个 SQL 查询获取给定列表中的所有项目:

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

但我不知道如何从 list_items 中获取列数量以及上述查询中的名称。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: sql sqlite


    【解决方案1】:

    您可以在下面尝试 - 使用加入

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

    【讨论】:

      【解决方案2】:

      为什么不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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-21
        • 2016-07-31
        相关资源
        最近更新 更多