2012-11-15 104 views
0

我想做這個查詢:MySQL查詢鮮明+多

SELECT * 
    FROM user k 
    INNER JOIN (
     SELECT id, tagName, b.guid, name, owner, publicKey 
     FROM noteTags a 
     INNER JOIN (
      SELECT * 
      FROM note 
      ORDER BY guid 
      LIMIT 0 , 12 
     )b ON a.guid = b.guid ORDER BY b.id DESC 
    )l ON k.owner = l.owner 

但我想它返回DISTINCT b.guids。表的

結構:

note 
| 
|=id 
|=name 
|=guid 
|=owner 
|=publicKey 

noteTags 
| 
|=guid 
|=tagName 

user 
| 
|=owner 
|=username 
|=auth 

基本上我想選擇所有的數據(與更深內加入限制),並返回不同的GUID

謝謝!

+0

這是一個例子,如果有兩個記錄具有相同的圖形用戶界面,下列哪一個會顯示爲'GUI = 1,Colname = 1'或'GUI = 1,Colname = 2'? –

+0

並不重要,兩者的值都相同 –

+0

所以只要只顯示1個GUI,就表示以下任何一項。 –

回答

0

這是我最初的回答,

SELECT * 
FROM note a 
     INNER JOIN user b 
      ON a.owner = b.owner 
     INNER JOIN notetags c 
      ON a.guid = b.guid 
     INNER JOIN 
     (
      SELECT guid, MAX(tagName) maxTag 
      FROM notetags 
      GROUP BY guid 
     ) d ON c.guid = d.guid AND 
       c.tagName = d.maxTag 
0

如何:

SELECT *
從用戶k
INNER JOIN(
SELECT ID,標籤名,b.guid,名稱,所有者, publicKey
FROM noteTags a
INNER JOIN(
select id,name,MIN(gu ID)作爲GUID,所有者公鑰
FROM音符
GROUP BY GUID
LIMIT 0,12 )B關於a.guid = b.guid ORDER BY b.id DESC
)升ON k.owner =升.owner