简体   繁体   English

SQL查询-使用附加动态列联接多个表

[英]SQL-Query - Join multiple tables with additional dynamic column

I need help for building a SQL-statement. 我需要有关构建SQL语句的帮助。 The Database-Schema looks like this: 数据库架构如下所示:

SQL模式

I did prepare the following SQL-Fiddle , which contains sample-data: http://sqlfiddle.com/#!9/831bab/1 我确实准备了下面的SQL-Fiddle ,其中包含示例数据: http : //sqlfiddle.com/#!9/831bab/1

As for the sample-data in the SQL-Fiddle, I want to query for the computername "Client02" and want to get the following result: 至于SQL小提琴中的样本数据,我想查询计算机名称“ Client02”,并希望得到以下结果:

Wanted result 1 想要的结果1

PrinterName   | PrintServer | PrinterActive | ComputerActive | isDefaultPrinter
PRT01_Zentral | DC01        | True          | True           | False
PRT02_BH      | DC01        | True          | True           | True

As for the sample-data in the SQL-Fiddle, I want to query for the computername "Client01" and want to get the following result: 至于SQL小提琴中的样本数据,我想查询计算机名称“ Client01”,并希望得到以下结果:

Wanted result 2 想要的结果2

PrinterName   | PrintServer | PrinterActive | ComputerActive | isDefaultPrinter
PRT01_Zentral | DC01        | True          | True           | True

As you see, I need to join all the tables and add something like a helper-column, which contains information about the default-printer. 如您所见,我需要连接所有表并添加类似于帮助程序列的内容,其中包含有关默认打印机的信息。 (True/False) (真假)

I started to build up the query, but I don't know how to proceed ... 我开始建立查询,但是我不知道如何进行...

SELECT printers.PrinterName, printers.PrintServer, printers.PrinterActive
FROM computermapping
LEFT JOIN computers ON computermapping.ComputerID = computers.ComputerID
LEFT JOIN printers ON computermapping.PrinterID = printers.PrinterID
LEFT JOIN computerdefaultprinter ON computers.ComputerID = computerdefaultprinter.ComputerID
WHERE computers.ComputerName = "Client02" 

I think my request contains all the information, which is needed. 我认为我的请求包含所有必要的信息。 The SQL-Fiddle has sample-data to easily reproduce it. SQL-Fiddle具有样本数据,可以轻松地重现它。 The WantedResults should show the target clearly. WantedResults应该清楚地显示目标。

EDIT: 编辑:
DBMS: MySQL DBMS:MySQL

What I did was add a if statement in your query, comparing the inner result (that can only be 1) to your outter select. 我所做的就是在查询中添加一条if语句,将内部结果(只能为1)与外部选择项进行比较。 There is a better way of achieving this probably but this answer reflects the queryresult's that you've asked. 有一种更好的方法可以实现此目的,但是此答案反映了您所询问的查询结果。

Edit: I'm assuming only a default printer per computer. 编辑:我假设每台计算机仅默认打印机。 Are you sure the wanted result 2 is correct? 您确定所需结果2是否正确? Because in the database, records are saying computer1 has 1 printerid (1) that is the default and that computer2 has 2 printers (1 and 2) and 2 is default. 因为在数据库中,记录说计算机1具有1默认的printerid(1),计算机2具有2打印机(1和2),而计算机2是默认打印机。

SELECT printers.PrinterName,
       printers.PrintServer,
       printers.PrinterActive,
       IF (
             (SELECT computerdefaultprinter.printerid
              FROM computerdefaultprinter
              WHERE computerdefaultprinter.computerid = computermapping.computerid) = computermapping.printerid,
           'true',
           'false') AS isDefaultPrinter
FROM computermapping
LEFT JOIN computers ON computermapping.ComputerID = computers.ComputerID
LEFT JOIN printers ON computermapping.PrinterID = printers.PrinterID
LEFT JOIN computerdefaultprinter ON computers.ComputerID = computerdefaultprinter.ComputerID
WHERE computers.ComputerName = "Client02"

You could use the following query to get the result that you need 您可以使用以下查询来获取所需的结果

SELECT printers.PrinterName, 
       printers.PrintServer, 
       printers.PrinterActive, 
       computers.ComputerActive, 
       CASE 
         WHEN computerdefaultprinter.PrinterID IS NULL THEN "false" 
         ELSE "true" 
       END AS isDefaultPrinter 
FROM   computermapping 
       LEFT JOIN computers 
              ON computermapping.ComputerID = computers.ComputerID 
       LEFT JOIN printers 
              ON computermapping.PrinterID = printers.PrinterID 
       LEFT JOIN computerdefaultprinter 
              ON computers.ComputerID = computerdefaultprinter.ComputerID 
                 AND printers.PrinterID = computerdefaultprinter.PrinterID 
WHERE  computers.ComputerName = "Client02" 

I've only added CASE...WHEN statement and modified join condition of your original query. 我只添加了CASE ... WHEN语句,并修改了原始查询的联接条件。 But I think instead of using table computerdefaultprinter , why don't you add column IsDefaultPrinter to table computermapping . 但我认为,为什么不使用表computerdefaultprinter而不是将IsDefaultPrinter列添加到表computermapping

CREATE TABLE `computermapping` (
  `ComputerMappingID` int(11) NOT NULL,
  `PrinterID` int(11) NOT NULL,
  `ComputerID` int(11) NOT NULL,
  `IsDefaultPrinter` int(1) NOT NULL DEFAULT 0,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM