【问题标题】:Understanding SQL queries: how to return the foreign table's information理解 SQL 查询:如何返回外部表的信息
【发布时间】:2021-03-03 00:56:03
【问题描述】:

我不太明白使用关系时 SQL 查询是如何工作的;

我正在构建一个基于数学的小型闪存卡应用程序。为了效率和学习,我希望不要有重复的问题和答案。

所以,我们最终得到以下表格;

addition
questionId | answerId | info


question
id | num_a | num_b


answer
id | res

我如何返回[num_a, num_b, res, info]

如何选择num_a < xinfo = y的附加题?

【问题讨论】:

    标签: sql inner-join where-clause


    【解决方案1】:

    你可以加入:

    select qu.num_a, qu.num_b, an.res, ad.info
    from addition ad
    inner join question qu on qu.id = ad.questionid
    inner join answer an on an.id = qd.answerid
    

    然后您可以使用where 子句设置您想要的过滤:

    where qu.num_a < x and ad.info = y
    

    当然xy 需要是正确的文字值,其数据类型与表列的数据类型匹配。

    【讨论】:

      【解决方案2】:

      只是为了扩展 GMB 对您的第二个查询的回答 - 您可以使用 WHERE 子句,例如:

      select qu.num_a, qu.num_b, an.res, ad.info
      from addition ad
      inner join question qu on qu.id = ad.questionid
      inner join answer an on an.id = qd.answerid
      WHERE qu.num_a < x AND ad.info = y
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-15
        • 2016-04-24
        • 1970-01-01
        • 2015-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多