0

使用以下 sql 进行搜索的 mysql 查询

SELECT societa.nome, documenti.document, soieditor.nominativo, document_revision.* 
FROM document_revision 
        JOIN societa ON societa.id = document_revision.societa 
        JOIN documenti ON documenti.id = document_revision.tipo_document 
        JOIN soieditor ON soieditor.id = document_revision.createdby 
        WHERE document_revision.id in(
        (
    (SELECT id from document_revision WHERE `description` LIKE '%f%' OR `tag` LIKE '%f%' OR `lingua` LIKE '%f%') 
    union 
    (SELECT doc_rev_id from document_metadata WHERE `met_key` LIKE '%f%' OR `meta_value` LIKE '%f%') 
    union 
    (SELECT doc_id from document_files WHERE `file_name` LIKE '%f%')))

给我以下错误'#1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在“联合”附近使用正确的语法(从 document_metadata WHERE met_keyLIKE '%fwdfdfdfd' at line 1' 中选择 doc_rev_id)

请帮我解决错误。

4

2 回答 2

1
SELECT societa.nome, 
    documenti.document, 
    soieditor.nominativo, 
    document_revision.* 
FROM document_revision 
    JOIN societa ON societa.id = document_revision.societa 
    JOIN documenti ON documenti.id = document_revision.tipo_document 
    JOIN soieditor ON soieditor.id = document_revision.createdby 
WHERE document_revision.id in
(
    SELECT id as ii
    from document_revision 
    WHERE description LIKE '%f%' OR tag LIKE '%f%' OR lingua LIKE '%f%' 

    union 

    SELECT doc_rev_id as ii
    from document_metadata 
    WHERE met_key LIKE '%f%' OR meta_value LIKE '%f%'

    union 

    SELECT doc_id as ii
    from document_files 
    WHERE `file_name` LIKE '%f%'
)
于 2013-10-02T13:49:39.477 回答
0

尝试以这种方式在联合链的第一个子查询上放置别名:

SELECT societa.nome, documenti.document, soieditor.nominativo, document_revision.*
FROM document_revision 
JOIN societa ON societa.id = document_revision.societa 
JOIN documenti ON documenti.id = document_revision.tipo_document 
JOIN soieditor ON soieditor.id = document_revision.createdby 
WHERE document_revision.id in(
(
    (SELECT id as myid -- alias named your output field
    from document_revision
    WHERE `description` LIKE '%f%' OR `tag` LIKE '%f%' OR `lingua` LIKE '%f%')
    union
    (SELECT doc_rev_id
    from document_metadata WHERE `met_key` LIKE '%f%' OR `meta_value` LIKE '%f%')
    union
    (SELECT doc_id from document_files WHERE `file_name` LIKE '%f%')
)
)
于 2013-10-02T13:51:02.970 回答