显然mySQL不支持完全外连接,但它确实是我需要的。我已经看过一些关于用工会模仿它的博客文章和文章,但这会删除重复项。有人可以帮我从这里出去吗?
这是包含完整外连接的查询(这里只完成了许多连接中的一个);我将如何将其转化为mySQL理解的内容?
SELECT DISTINCT n.title, nr.teaser, n.nid, DATE_FORMAT(FROM_UNIXTIME(n.created), '%M %e, %Y') AS date, f.filepath AS image,tn_img.tid as image_tid
FROM node n
JOIN node_revisions nr ON n.nid = nr.nid
LEFT JOIN content_field_related_images cfri ON (n.nid = cfri.nid AND cfri.delta = 0)
LEFT JOIN content_field_att_file cfaf ON cfri.field_related_images_nid = cfaf.nid
LEFT JOIN files f ON cfaf.field_att_file_fid = f.fid
JOIN term_node tn2 ON n.nid = tn2.nid
FULL OUTER JOIN term_node tn_img ON cfri.field_related_images_nid = tn_img.nid
WHERE n.status = 1
AND n.type = 'article'
AND nr.body LIKE '%kimberly-clark%'
AND tn2.tid = 143
ORDER BY n.created DESC LIMIT 3
答案 0 :(得分:1)
你基本上做了LEFT和RIGHT JOIN的联盟。
你实际上有一些有趣的皱纹,你也想将行限制为3.要解决这个问题,你需要
更新可悲的是,除非我弄错了你不能直接在UNION中这样做,所以你需要在UNION之前添加另一层内联视图
UNION内部的LIMITS将提供一些性能优势,之后的限制将为您提供正确的结果。
SELECT title,
teaser,
nid,
DATE,
image,
image_tid
FROM (SELECT title,
teaser,
nid,
DATE,
image,
image_tid,
created
FROM (SELECT DISTINCT n.title,
nr.teaser,
n.nid,
Date_format(From_unixtime(n.created),
'%M %e, %Y') AS
DATE,
f.filepath
AS
image,
tn_img.tid
AS
image_tid
,
n.created
FROM node n
JOIN node_revisions nr
ON n.nid = nr.nid
LEFT JOIN content_field_related_images cfri
ON ( n.nid = cfri.nid
AND cfri.delta = 0 )
LEFT JOIN content_field_att_file cfaf
ON cfri.field_related_images_nid = cfaf.nid
LEFT JOIN files f
ON cfaf.field_att_file_fid = f.fid
JOIN term_node tn2
ON n.nid = tn2.nid
LEFT OUTER JOIN term_node tn_img
ON cfri.field_related_images_nid = tn_img.nid
WHERE n.status = 1
AND n.TYPE = 'article'
AND nr.body LIKE '%kimberly-clark%'
AND tn2.tid = 143
ORDER BY n.created DESC
LIMIT 3) tleft
UNION
SELECT title,
teaser,
nid,
DATE,
image,
image_tid,
created
FROM (SELECT DISTINCT n.title,
nr.teaser,
n.nid,
Date_format(From_unixtime(n.created),
'%M %e, %Y') AS
DATE,
f.filepath
AS
image,
tn_img.tid
AS
image_tid
,
n.created
FROM node n
JOIN node_revisions nr
ON n.nid = nr.nid
LEFT JOIN content_field_related_images cfri
ON ( n.nid = cfri.nid
AND cfri.delta = 0 )
LEFT JOIN content_field_att_file cfaf
ON cfri.field_related_images_nid = cfaf.nid
LEFT JOIN files f
ON cfaf.field_att_file_fid = f.fid
JOIN term_node tn2
ON n.nid = tn2.nid
RIGHT OUTER JOIN term_node tn_img
ON cfri.field_related_images_nid = tn_img.nid
WHERE n.status = 1
AND n.TYPE = 'article'
AND nr.body LIKE '%kimberly-clark%'
AND tn2.tid = 143
ORDER BY n.created DESC
LIMIT 3) tright) t
ORDER BY created DESC
LIMIT 3
<强>更新强> 在这里使用spencer7593和ypercube建议是使用两个UNION ALL语句而没有内联视图的替代方法。
SELECT DISTINCT n.created,
n.title,
nr.teaser,
n.nid,
Date_format(From_unixtime(n.created), '%M %e, %Y') AS DATE,
f.filepath AS image,
tn_img.tid AS image_tid
FROM node n
JOIN node_revisions nr
ON n.nid = nr.nid
LEFT JOIN content_field_related_images cfri
ON ( n.nid = cfri.nid
AND cfri.delta = 0 )
LEFT JOIN content_field_att_file cfaf
ON cfri.field_related_images_nid = cfaf.nid
LEFT JOIN files f
ON cfaf.field_att_file_fid = f.fid
JOIN term_node tn2
ON n.nid = tn2.nid
LEFT OUTER JOIN term_node tn_img
ON cfri.field_related_images_nid = tn_img.nid
WHERE n.status = 1
AND n.TYPE = 'article'
AND nr.body LIKE '%kimberly-clark%'
AND tn2.tid = 143
UNION ALL
SELECT DISTINCT n.created,
n.title,
nr.teaser,
n.nid,
Date_format(From_unixtime(n.created), '%M %e, %Y') AS DATE,
f.filepath AS image,
tn_img.tid AS image_tid
FROM node n
JOIN node_revisions nr
ON n.nid = nr.nid
LEFT JOIN content_field_related_images cfri
ON ( n.nid = cfri.nid
AND cfri.delta = 0 )
LEFT JOIN content_field_att_file cfaf
ON cfri.field_related_images_nid = cfaf.nid
LEFT JOIN files f
ON cfaf.field_att_file_fid = f.fid
JOIN term_node tn2
ON n.nid = tn2.nid
RIGHT JOIN term_node tn_img
ON cfri.field_related_images_nid = tn_img.nid
WHERE n.status = 1
AND n.TYPE = 'article'
AND nr.body LIKE '%kimberly-clark%'
AND tn2.tid = 143
AND cfri.field_related_images_nid IS NULL
ORDER BY 1 DESC
LIMIT
3