0

我有 3 张桌子:

users:

uid int(11) - userid(primary key, auto_increment)
name varchar(255)
pass varchar(64)
created int(11)

projects:

pid int(11) .....
name varchar(150)
description varchar(255)
created int(11)

users_projects:

uid int(11) - user id
pid int(11) - product id

查询是什么:为具有特定名称的用户显示按名称排序的项目(名称和描述)?

这是我到目前为止所得到的:

SEECT name,description FROM projects ORDER BY name ASC
4

2 回答 2

2

看起来你只需要加入表格。这@Name是一个包含您想要项目详细信息的用户名的参数:

SELECT projects.Name,
       projects.Description
FROM   projects
       INNER JOIN users_projects
         ON projects.pid = users_projects.pid
       INNER JOIN Users
         ON users_projects.UId = Users.UId
WHERE  Users.Name = @Name
ORDER  BY projects.Name ASC
于 2013-02-11T12:00:49.977 回答
2
SELECT  a.*, b.*
FROM    users a
        INNER JOIN users_projects b
            ON a.uid = b.uid
        INNER JOIN projects c
            ON b.pid = c.pid
ORDER   BY a.Name ASC

要进一步了解有关联接的更多信息,请访问以下链接:

于 2013-02-11T11:58:33.473 回答