带有 union 和 in 子句的 Mysql 错误
Posted
技术标签:
【中文标题】带有 union 和 in 子句的 Mysql 错误【英文标题】:Mysql error with union and in clause 【发布时间】:2013-10-02 13:43:08 【问题描述】:使用以下 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 服务器版本相对应的手册,以了解在 'union (SELECT doc_rev_id from document_metadata WHERE met_key
LIKE '%fwdfdfdfd' at line 1' 附近使用的正确语法"
请帮我解决错误。
【问题讨论】:
【参考方案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%'
)
【讨论】:
【参考方案2】:尝试在你的联合链的第一个子查询上放一个别名,这样:
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%')
)
)
【讨论】:
以上是关于带有 union 和 in 子句的 Mysql 错误的主要内容,如果未能解决你的问题,请参考以下文章
强制 MySQL 从 WHERE IN 子句返回重复项而不使用 JOIN/UNION?