加入SQL查询不起作用

时间:2018-01-21 17:14:03

标签: mysql sql database

我有一张表app_form_data_audit_trail,我需要将其加入另一张表app_fd_listofcomponents

- 首先需要从列中提取外键,然后在第一个表与第二个表连接之前整理其他一些数据。

- 需要过滤掉第一张表中的一些记录。

我做了以下sql查询但是收到错误。我创建了一个sqlfiddle here用于故障排除..

感谢您的帮助: - )

SELECT DATETIME
    ,username
    ,CASE 
        WHEN locate('operation', DATA) > 0
            THEN substring_index(substring_index(DATA, '"operation":"', - 1), '"', 1)
        ELSE 0
        END AS operation
    ,CASE 
        WHEN locate('operation', DATA) > 0
            THEN substring_index(substring_index(DATA, '"opdetails":"', - 1), '"', 1)
        ELSE 0
        END AS opdetails
    ,CASE 
        WHEN locate('operation', DATA) > 0
            THEN substring_index(substring_index(DATA, '"componentName":"', - 1), '"', 1)
        ELSE 0
        END AS componentid
    ,CASE 
        WHEN locate('operation', DATA) > 0
            THEN substring_index(substring_index(DATA, '"package":"', - 1), '"', 1)
        ELSE 0
        END AS package
FROM app_form_data_audit_trail
WHERE DATA LIKE "%Operation%"
INNER JOIN app_fd_listofcomponents ON app_form_data_audit_trail.componentid = app_fd_listofcomponents.id

2 个答案:

答案 0 :(得分:0)

FROM app_form_data_audit_trail INNER JOIN app_fd_listofcomponents ON app_form_data_audit_trail.componentid = app_fd_listofcomponents.id 数据类似于“%Operation%”

答案 1 :(得分:0)

感谢所有人,最后我来编写如下查询并且它有效!

SELECT
app_form_data_audit_trail.datetime,
app_form_data_audit_trail.username,
app_form_data_audit_trail.data,
app_fd_listofcomponents.c_component,
substring_index(substring_index(app_form_data_audit_trail.data,'"operation":"',-1),'"',1) AS operation,
substring_index(substring_index(app_form_data_audit_trail.data,'"opdetails":"',-1),'"',1) AS opdetails,
substring_index(substring_index(app_form_data_audit_trail.data,'"package":"',-1),'"',1) AS package
FROM app_form_data_audit_trail
JOIN app_fd_listofcomponents
ON app_form_data_audit_trail.data LIKE CONCAT ('%',app_fd_listofcomponents.id,'%')