简体   繁体   English

SQL Join ON 子句有效引用别名

[英]SQL Join ON clause valid referencing on aliases

I am generating an SQL query and I need to get the data from a JSON stored on a field of my table.我正在生成一个 SQL 查询,我需要从存储在我的表的一个字段中的JSON中获取数据。 It goes something like this它是这样的

SELECT creation.INSERT_DATE as applicationDateTime,
REPLACE(json_extract(creation.FACILITY_DATA, '$.loanType'), '"', '') AS loanType,
lookup_detail.DETAIL_DESCRIPTION AS financingType 
FROM creation 
LEFT JOIN lookup_detail ON lookup_detail.DETAIL_CODE = loanType

So basically I am trying to get put a connection with tables creation and lookup_detail through field FACILITY_DATA which has JSON data and alias of loanType to reference against DETAIL_CODE field.所以基本上我试图通过字段FACILITY_DATA建立与表创建lookup_detail的连接,该字段具有JSON数据和loanType的别名以引用DETAIL_CODE字段。 However, I get this error但是,我收到此错误

code:"ER_BAD_FIELD_ERROR"
errno:1054
sqlMessage:"Unknown column 'loanType' in 'on clause'"
sqlState:"42S22"

Is there anything I could do to work on this?有什么我可以做的吗? I tried to search what are the valid reference to ON clause of JOIN operation, but I only get the typical ways.我试图搜索对JOIN操作的ON子句的有效引用是什么,但我只得到了典型的方法。

Either repeat the expression in the ON clause, or join with a subquery.要么重复ON子句中的表达式,要么加入子查询。

SELECT c.applicationDateTime, c.loanType, l.financingType
FROM (
    SELECT INSERT_DATE as applicationDateTime,
            REPLACE(json_extract(creation.FACILITY_DATA, '$.loanType'), '"', '') AS loanType
    FROM creation
) AS c
JOIN lookup_detail AS l ON l.DETAIL_CODE = c.loanType

Also, you probably should be using JSON_UNQUOTE() rather than REPLACE() .此外,您可能应该使用JSON_UNQUOTE()而不是REPLACE() Or you can use the ->> operator, which extracts and unquotes in one step.或者您可以使用->>运算符,一步提取和取消引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM