2013-02-11 236 views
0

我有3個表:SQL SELECT查詢

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 
+1

你正在使用哪些DBMS?甲骨文? Postgres的? – 2013-02-11 11:57:55

+0

SQLyog的社區 – emcee22 2013-02-11 11:58:57

+0

SQLyog的是不是一個DBMS。 – 2013-02-11 12:27:04

回答

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 

爲了進一步獲得更多的知識有關加入,請訪問以下鏈接:

+0

非常感謝。 – emcee22 2013-02-11 12:00:57

+0

歡迎您':D' – 2013-02-11 12:03:32

+0

這些可視化表示需要重視他們一些注意事項。 – Strawberry 2013-02-11 12:10:46

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