2011-05-20 40 views
2

(已貼在drupal.stackexchange.com沒有響應;因爲這個問題是MySQL特有希望這裏有人能幫助... ...)查詢幫助;我需要一個子查詢嗎?

我繼承了這個代碼,在我們的主頁上運行。它用於自定義顯示特定的相關信息。以下是如何設置:

我們有一篇文章。它有一系列節點引用字段,允許我們鏈接相關圖像(實際上是節點本身,其中包含爆頭和一些文本;它不是實際附加的圖像)。每個爆頭都有通過ImageCache創建的縮略圖,以便我們可以在搜索結果和列表類型頁面上顯示一個小版本。

我們有一個新的要求,即如果特定文章的相關圖像節點具有與其相關的特定分類術語(tid),則它應該以與其他圖像不同的方式顯示。

現有的查詢(我沒寫)看起來是這樣的:

SELECT DISTINCT n.title, nr.teaser, n.nid, DATE_FORMAT(FROM_UNIXTIME(n.created), '%M %e, %Y') AS date, f.filepath AS image, cfri.field_related_images_nid as image_id 

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 

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 

這返回記錄,看起來像這樣:

TITLE   | TEASER   | NID | DATE   | IMAGE       | IMAGE_ID 
========================================================================================================== 
Exec Profile | Could you please | 67491 | April 29, 2011 | sites/default/files/kcjones.jpg | 67572 
Unilver Ads | Unilever topped | 67421 | April 20, 2011 | sites/default/files/unilever.jpg | 66889 

這是一切都很好。但是我需要做的是(在同一個查詢中,也以某種方式)也返回來自term_node表的image_id的tid。就其本身的查詢是簡單的單個圖像節點:

SELECT tid FROM term_node WHERE nid = 67572 

但是我怎麼修改現有的查詢基於查詢的主要部分發現image_id在TID帶來了什麼?我猜我需要某種子查詢,但這是拉伸我的sql排印的極限...

--------------------- -------------------------------------------------- -------------------

UPDATE每請求時,這裏是在查詢中使用的所有表的表結構:

-- Table structure for table `content_field_att_file` 
-- 

CREATE TABLE IF NOT EXISTS `content_field_att_file` (
    `vid` int(10) unsigned NOT NULL DEFAULT '0', 
    `nid` int(10) unsigned NOT NULL DEFAULT '0', 
    `delta` int(10) unsigned NOT NULL DEFAULT '0', 
    `field_att_file_fid` int(11) DEFAULT NULL, 
    `field_att_file_list` tinyint(4) DEFAULT NULL, 
    `field_att_file_data` text, 
    PRIMARY KEY (`vid`,`delta`), 
    KEY `nid` (`nid`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `content_field_related_images` 
-- 

CREATE TABLE IF NOT EXISTS `content_field_related_images` (
    `vid` int(10) unsigned NOT NULL DEFAULT '0', 
    `nid` int(10) unsigned NOT NULL DEFAULT '0', 
    `delta` int(10) unsigned NOT NULL DEFAULT '0', 
    `field_related_images_nid` int(10) unsigned DEFAULT NULL, 
    PRIMARY KEY (`vid`,`delta`), 
    KEY `nid` (`nid`), 
    KEY `field_related_images_nid` (`field_related_images_nid`), 
    KEY `delta` (`delta`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `files` 
-- 

CREATE TABLE IF NOT EXISTS `files` (
    `fid` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `uid` int(10) unsigned NOT NULL DEFAULT '0', 
    `filename` varchar(255) NOT NULL DEFAULT '', 
    `filepath` varchar(255) NOT NULL DEFAULT '', 
    `filemime` varchar(255) NOT NULL DEFAULT '', 
    `filesize` int(10) unsigned NOT NULL DEFAULT '0', 
    `status` int(11) NOT NULL DEFAULT '0', 
    `timestamp` int(10) unsigned NOT NULL DEFAULT '0', 
    PRIMARY KEY (`fid`), 
    KEY `uid` (`uid`), 
    KEY `status` (`status`), 
    KEY `timestamp` (`timestamp`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=100810 ; 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `node` 
-- 

CREATE TABLE IF NOT EXISTS `node` (
    `nid` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `vid` int(10) unsigned NOT NULL DEFAULT '0', 
    `type` varchar(32) NOT NULL DEFAULT '', 
    `language` varchar(12) NOT NULL DEFAULT '', 
    `title` varchar(255) NOT NULL DEFAULT '', 
    `uid` int(11) NOT NULL DEFAULT '0', 
    `status` int(11) NOT NULL DEFAULT '1', 
    `created` int(11) NOT NULL DEFAULT '0', 
    `changed` int(11) NOT NULL DEFAULT '0', 
    `comment` int(11) NOT NULL DEFAULT '0', 
    `promote` int(11) NOT NULL DEFAULT '0', 
    `moderate` int(11) NOT NULL DEFAULT '0', 
    `sticky` int(11) NOT NULL DEFAULT '0', 
    `tnid` int(10) unsigned NOT NULL DEFAULT '0', 
    `translate` int(11) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`nid`), 
    UNIQUE KEY `vid` (`vid`), 
    KEY `node_changed` (`changed`), 
    KEY `node_created` (`created`), 
    KEY `node_moderate` (`moderate`), 
    KEY `node_promote_status` (`promote`,`status`), 
    KEY `node_status_type` (`status`,`type`,`nid`), 
    KEY `node_title_type` (`title`,`type`(4)), 
    KEY `node_type` (`type`(4)), 
    KEY `uid` (`uid`), 
    KEY `tnid` (`tnid`), 
    KEY `translate` (`translate`), 
    KEY `status` (`status`,`type`,`created`), 
    KEY `status_2` (`status`,`type`,`created`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=102682 ; 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `node_revisions` 
-- 

CREATE TABLE IF NOT EXISTS `node_revisions` (
    `nid` int(10) unsigned NOT NULL DEFAULT '0', 
    `vid` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `uid` int(11) NOT NULL DEFAULT '0', 
    `title` varchar(255) NOT NULL DEFAULT '', 
    `body` longtext NOT NULL, 
    `teaser` longtext NOT NULL, 
    `log` longtext NOT NULL, 
    `timestamp` int(11) NOT NULL DEFAULT '0', 
    `format` int(11) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`vid`), 
    KEY `nid` (`nid`), 
    KEY `uid` (`uid`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=102690 ; 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `term_node` 
-- 

CREATE TABLE IF NOT EXISTS `term_node` (
    `nid` int(10) unsigned NOT NULL DEFAULT '0', 
    `vid` int(10) unsigned NOT NULL DEFAULT '0', 
    `tid` int(10) unsigned NOT NULL DEFAULT '0', 
    PRIMARY KEY (`tid`,`vid`), 
    KEY `vid` (`vid`), 
    KEY `nid` (`nid`), 
    KEY `tid` (`tid`,`nid`), 
    KEY `tid_2` (`tid`,`nid`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 
+0

我不明白的問題。你已經加入term_node。爲什麼你不能在select子句中添加tid?或者你的意思是你需要查找一個不同的term_node記錄,然後是你已經加入的那個記錄?如果是這樣,這個記錄的選擇標準是什麼?在你的例子中,你只是說nid =。這與已經在選擇中的nid是否相同?如果不是,那是什麼? – Jay 2011-05-20 16:41:05

+0

因爲該連接正在返回n.nid的tid。我需要cfri.field_related_images_nid的tid,它是初始選擇語句的一部分。 – EmmyS 2011-05-20 16:48:18

回答

2

你可以爲term_node表添加第二個聯接並將該列添加到選擇列表中:

SELECT DISTINCT n.title, nr.teaser, n.nid, DATE_FORMAT(FROM_UNIXTIME(n.created), '%M %e, %Y') AS date, f.filepath AS image, cfri.field_related_images_nid as image_id, 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 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 

這是重要的一行:LEFT JOIN term_node tn_img on cfri.field_related_images_nid = tn_img.nid,不要忘記將列添加到選擇列表!

這可能是我的查詢是錯的,我們沒有對錶結構多的信息,如果沒有工作,發佈的數據庫模式:)

更新,以反映意見討論

+0

我會先嚐試你的查詢;這是一個drupal安裝,因此數據庫非常龐大。如果這不起作用,我會嘗試輸出相關表格的模式。 – EmmyS 2011-05-20 16:51:40

+0

不,那返回超過3000條記錄,應該只有3個記錄。我會嘗試獲取表格信息。 – EmmyS 2011-05-20 16:53:34

+0

@EmmyS嘗試在添加的行中用'INNER JOIN'替換'JOIN'。 – krtek 2011-05-20 17:51:04

0

我可能失去了一些東西,但這應該工作

SELECT DISTINCT n.title, nr.teaser, n.nid, **tn2.tid** DATE_FORMAT(FROM_UNIXTIME(n.created), '%M %e, %Y') AS date, f.filepath AS image, cfri.field_related_images_nid as image_id 

FROM node n JOIN node_revisions nr ON n.nid = nr.nid 
JOIN term_node tn tn2 ON n.nid = tn2.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 

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

請參閱初始文章的評論; tn2.tid包含在where子句中以將結果限制爲n。記錄的tid爲143.我希望包含cfri的結果,對tid沒有限制,只要nid與記錄集中返回的cfri.related_images_nid相匹配即可。 – EmmyS 2011-05-20 16:50:46