1

我有四个表,想创建一个新表,每个客户端只有一行。

客户

id  |   fname  |   lname   |   city  |  company_id

1   |   kim    |   smith   |   jhb   |  1
2   |   tom    |   jones   |   ct    |  1
3   |   john   |   short   |   pe    |  2

CLIENT_DIVISION

id   |  client_id  |  division_id

1    |  1          |  1
2    |  1          |  2
3    |  2          |  1
4    |  2          |  3
5    |  2          |  4
6    |  3          |  4
7    |  3          |  2

分配

id  |  name

1   |  estate
2   |  property
3   |  litigation
4   |  commercial

公司

id  | name

1   | compA
2   | compB

查询部门 = 房地产时所需的表结果

id  |  fname  | lname  | city  |  company_name

1   |  kim    | smith  | jhb   |  compA
2   |  tom    | jones  | ct    |  compA

我希望能够创建一个新表,每个客户端有一行,将除法参数传递给查询。传递的参数将来自用户在提交查询之前选择的复选框。如何为此创建连接查询?

4

1 回答 1

0

我希望这个查询能帮助你:

SELECT DISTINCT     CLIENT.id, CLIENT.fname, CLIENT.lname, CLIENT.city, COMPANY.name
FROM         CLIENT INNER JOIN
                  CLIENT_DIVISION ON CLIENT.id = CLIENT_DIVISION.client_id INNER JOIN
                  COMPANY ON CLIENT.company_id = COMPANY.id INNER JOIN
                  DIVISION ON CLIENT_DIVISION.division_id = DIVISION.id
WHERE     (DIVISION.id = @division_id)

您需要将 @division_id 替换为您的值。

于 2013-04-30T08:09:18.967 回答