2013-09-23 22 views
0

我有一個漫長的MySQL查詢我試圖使用FULL OUTER JOIN上,這並不在MySQL存在:不正確的密鑰文件的外部連接邏輯在MySQL

select c.cat_id,c.cat_name as cat_name, 
c.cat_desc, c.cat_image, mi.filename, 
l.link_id, l.user_id, l.address,l.city, 
l.country,l.link_created,l.link_desc, 
l.email,l.fax,l.link_hits, l.link_modified, 
l.link_name,l.postcode, l.price,l.link_rating, 
l.state,l.telephone,l.link_votes, 
l.website, l.link_id, l.link_visited, cf.value 
from j25_mt_cats as c, 
j25_mt_links as l 
LEFT JOIN j25_mt_cfvalues AS cf ON (cf.link_id = l.link_id), 
j25_mt_images AS mi, 
j25_mt_cl as cl 
where cf.cf_id = 40 and cl.link_id = l.link_id 
AND mi.link_id = l.link_id AND mi.ordering < 2 
AND c.cat_id = cl.cat_id and c.cat_published = 1 
AND c.cat_approved = 1 and l.link_published = 1 and l.link_approved = 1 
ORDER BY RAND() DESC; 

的LEFT JOIN上面是我正在尋找的地方,而不是使用FULL OUTER JOIN邏輯。我知道它必須在UNION中使用多個JOIN,但是我的嘗試導致了各種語法錯誤。

什麼是正確的語法將上述更改爲完整的外部連接邏輯?

我想這一點:

select c.cat_id,c.cat_name as cat_name, 
c.cat_desc, c.cat_image, mi.filename, 
l.link_id, l.user_id, l.address,l.city, 
l.country,l.link_created,l.link_desc, 
l.email,l.fax,l.link_hits, l.link_modified, 
l.link_name,l.postcode, l.price,l.link_rating, 
l.state,l.telephone,l.link_votes, 
l.website, l.link_id, l.link_visited, cf.value 
from j25_mt_cats as c, 
j25_mt_links as l 
LEFT OUTER JOIN j25_mt_cfvalues AS cf ON (cf.link_id = l.link_id), 
j25_mt_images AS mi, 
j25_mt_cl as cl 
UNION ALL 
select c.cat_id,c.cat_name as cat_name, 
c.cat_desc, c.cat_image, mi.filename, 
l.link_id, l.user_id, l.address,l.city, 
l.country,l.link_created,l.link_desc, 
l.email,l.fax,l.link_hits, l.link_modified, 
l.link_name,l.postcode, l.price,l.link_rating, 
l.state,l.telephone,l.link_votes, 
l.website, l.link_id, l.link_visited, cf.value 
FROM j25_mt_cats as c, 
j25_mt_links as l 
RIGHT OUTER JOIN j25_mt_cfvalues AS cf ON cf.link_id = l.link_id, 
j25_mt_images AS mi, 
j25_mt_cl as cl 
where cf.cf_id = 40 and cl.link_id = l.link_id 
AND mi.link_id = l.link_id AND mi.ordering < 2 
AND c.cat_id = cl.cat_id and c.cat_published = 1 
AND c.cat_approved = 1 and l.link_published = 1 and l.link_approved = 1 
AND cf.link_id IS NULL 
ORDER BY RAND() DESC; 

但它需要幾分鐘來運行,然後返回#126 - Incorrect key file for table '/tmp/#sql_4f82_0.MYI'; try to repair it 時,我覺得我更接近正確的語法。磁盤未滿或甚至關閉。這也不是一張表,它只是我在phpMyAdmin中運行的一個查詢。

+0

你應該看到這個http://stackoverflow.com/questions/4796872/full-outer-join-in-mysql –

+0

@ThịtBòTái是的,這是我嘗試的語法,我得到語法錯誤和排序。編輯我的問題。 – Zeno

+0

hi @Zeno,你的桌子被撞壞了,請修復它,這是你可以參考的參考鏈接,http://dev.mysql.com/doc/refman/5.0/en/repair-table.html –

回答

1

當您填充臨時目錄時,在您的情況下,您的查詢可能會創建巨大的臨時文件時,會發生此問題。

除了優化您的查詢,請嘗試在您的查詢過程中監視臨時目錄所在的分區show variables like 'tmpdir';

+0

是的,在'/ tmp'中創建的3GB文件已被確認。 – Zeno

1

如果可能的話,請提供表的設計,樣柱,未知列我想的問題是因爲你的底部聯接缺少一些投

select c.cat_id,c.cat_name as cat_name, 
c.cat_desc, c.cat_image, mi.filename, 
l.link_id, l.user_id, l.address,l.city, 
l.country,l.link_created,l.link_desc, 
l.email,l.fax,l.link_hits, l.link_modified, 
l.link_name,l.postcode, l.price,l.link_rating, 
l.state,l.telephone,l.link_votes, 
l.website, l.link_id, l.link_visited, cf.value 
from j25_mt_cats as c, 
j25_mt_links as l 
LEFT OUTER JOIN j25_mt_cfvalues AS cf ON (cf.link_id = l.link_id) 

//你MI這裏missng [開始]

j25_mt_images AS mi, 
j25_mt_cl as cl 
where cf.cf_id = 40 and cl.link_id = l.link_id 
AND mi.link_id = l.link_id AND mi.ordering < 2 
AND c.cat_id = cl.cat_id and c.cat_published = 1 
AND c.cat_approved = 1 and l.link_published = 1 and l.link_approved = 1 
AND cf.link_id IS NULL 

// END

UNION ALL 
select c.cat_id,c.cat_name as cat_name, 
c.cat_desc, c.cat_image, mi.filename, 
l.link_id, l.user_id, l.address,l.city, 
l.country,l.link_created,l.link_desc, 
l.email,l.fax,l.link_hits, l.link_modified, 
l.link_name,l.postcode, l.price,l.link_rating, 
l.state,l.telephone,l.link_votes, 
l.website, l.link_id, l.link_visited, cf.value 

//ü忘了定義C,L,CF在這裏

FROM j25_mt_cats as c, j25_mt_links as l 
RIGHT OUTER JOIN j25_mt_cfvalues as cf ON cf.link_id = l.link_id, 
j25_mt_images AS mi, 
j25_mt_cl as cl 
where cf.cf_id = 40 and cl.link_id = l.link_id 
AND mi.link_id = l.link_id AND mi.ordering < 2 
AND c.cat_id = cl.cat_id and c.cat_published = 1 
AND c.cat_approved = 1 and l.link_published = 1 and l.link_approved = 1 
AND cf.link_id IS NULL 
ORDER BY RAND() DESC; 
+0

非常感謝,奇怪的是我遇到了一個錯誤。更新我的問題。 – Zeno

+0

top query missing mi〜 –

+0

奇怪的是,這使得它嘗試查詢分鐘,然後錯誤#126 - 錯誤的表'/tmp/#sql_4f82_0.MYI'的密鑰文件;嘗試修復它' – Zeno