2011-05-20 81 views
0

我無法弄清楚爲什麼我無法在第二個表中選擇多個列。這裏是我的表:MySQL複合連接 - 無法在第二個表中選擇多個列

Computers: 
---------- 
id, cond, type 

Images: 
------- 
id, foreignid, name, width, height, def 

這裏是我的select語句:

SELECT 
    id, cond, type, 
    (SELECT name, width, height FROM images WHERE foreignid = computers.id ORDER BY def DESC LIMIT 1) AS image 
FROM computers 

這是我收到的錯誤:

Operand should contain 1 column(s) 

回答

2

你試圖做這樣的事情?

select c.id, c.cond, c.type, i.name, i.width, i.height from computers c 
left join images i on i.foreignid=c.id 
order by i.def desc limit 1 

編輯: 但加盟條款取決於exactely你想要什麼。 如果你想要的所有計算機,有他們的圖像,或者不使用

computers left join images 

如果你想所有的圖像,有他們的計算機或不使用

computers right join images 

如果你想與圖像和圖像的計算機只有電腦使用

computers inner join images 

如果你想要的所有計算機,所有圖像使用

computer outer join images 
+0

我想要所有的電腦和只有一個圖像,有默認圖像(def列如果其1) – 2011-05-20 13:39:20

+0

然後使用我的文章的第一條語句。 – Hyperboreus 2011-05-20 13:50:42

+0

謝謝,我添加了i.foreignid = c.id和i.def> 0,並刪除了限制語句以完成查詢。再次Stackoverflow FTW。 – 2011-05-20 15:08:03

0
SELECT comp.id, comp.cond, comp.type, img.name, img.height, img.width FROM computers comp left join image img on img.foreignid = comp.id 

將返回計算機及其相關圖像,如果這是您正在尋找。

相關問題