0

我想做这个查询:

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

基本上我想选择所有数据(对更深的内部连接有限制)并返回 DISTINCT guid

谢谢!

4

2 回答 2

0

怎么样:

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

于 2012-11-15T05:32:25.113 回答
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
于 2012-11-15T03:49:56.033 回答