【问题标题】:How to represent sql joins in JSON format? [closed]如何以 JSON 格式表示 sql 连接? [关闭]
【发布时间】:2020-12-29 10:19:33
【问题描述】:

我正在寻找一种以 JSON 格式表示 SQL 连接的方法。

我能想到的一种方法是:

{
    type: 'select',
    table: 'table1',
    innerjoin: {
        table2: {
            on: [userid', 'userId'] //table1Key,table2Key
        }
    }
}

但问题是如果 N 之间有连接怎么办。表?如果连接基于多个列怎么办?

例如:

SELECT x1,x2...., FROM table1 t1 
INNER JOIN table2 t2 on t1.Id=t2.Id 
INNER JOIN table3 t3 on t1.id=t3.Id  
LEFT JOIN table4 t4 on t3.Id=t4.Id and t3.name=t4.name

【问题讨论】:

  • 这比您想象的要复杂得多。 SQL 语法丰富且富有表现力。您可能想要使用 ORM 而不是尝试重新发明轮子。
  • 这最终会变得非常复杂,因为在查询和加入时有很多选项可用。
  • 而且Stack Overflow 不是一般设计讨论的地方。
  • @GMB 我很清楚这可能很复杂。但是假设我们消除了其他用例,例如使用除= 或任何内置函数之外的任何运算符。我能够表示两个表之间的连接。我只想将其扩展为最多 4 张桌子。你觉得可以吗?
  • 这更适合一个论坛,在那里您可以进行头脑风暴会议以提出设计。

标签: javascript sql json database ansi


【解决方案1】:

我会采用如下的数组方法:

{
    type: 'select',
    table: 'table1',
    joins: [{
        type : 'inner'
        from :'table1'
        to :'table2'
        on : { 'userId':'userId' // property belongs to from value belongs to to table
            'secondColumn':'secondOne'
           }
       }
}

【讨论】:

  • @Barmar 是的,但在更复杂的查询中,它并不总是外部对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-24
  • 1970-01-01
  • 2016-06-19
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多