简体   繁体   English

如何从两个表中进行选择并在SQL中匹配两列?

[英]How to SELECT FROM two tables and match two columns in SQL?

So what i'm trying to do is to SELECT all records from table1 that has a certain Project number. 所以我想做的是从具有特定项目编号的table1中选择所有记录。 Then i will JOIN another table and get only one column named "FloorId" that is equal to both Project AND Element in table1. 然后,我将联接另一个表并仅获得一个名为“ FloorId”的列,该列等于table1中的Project AND Element。

(SELECT Project FROM dbo.IMP_MODEL_GEOMETRY WHERE dbo.IMP_GEOMETRY.Project = dbo.IMP_ELEMENT.Project AND dbo.IMP_GEOMETRY.Element = dbo.IMP_ELEMENT.Element)

In the dbo.IMP_GEOMETRY table i will get several results on Project and Element, so maby i can just select the first one.. DISTINCT? 在dbo.IMP_GEOMETRY表中,我将在Project和Element上获得多个结果,因此maby我可以选择第一个。

This is what i have tried: 这是我尝试过的:

$sql = "SELECT * FROM dbo.IMP_ELEMENT WHERE Project LIKE '%$objNr%'
            INNER JOIN dbo.IMP_MODEL_GEOMETRY ON dbo.IMP_ELEMENT.Project = dbo.IMP_MODEL_GEOMETRY.Project";

I am new to SQL and don't really know what the "." 我是SQL的新手,并不真正知道“。”是什么。 stands for in "dbo.IMP". 在“ dbo.IMP”中代表。 The table name is: dbo.IMP_ELEMENT (I have seen querys where the dot marks the table and after the dot is the column name. But in this case the dot doesn't represent both table and column, it's just a dot in the table name. 表名是:dbo.IMP_ELEMENT(我看到查询在点标记表的地方,而点后面是列名。但是在这种情况下,点既不代表表又代表列,它只是表中的一个点名称。

Except in the JOIN where Project is the column (dbo.IMP_MODEL_GEOMETRY.Project) 除了JOIN(其中Project是列)(dbo.IMP_MODEL_GEOMETRY.Project)

The table i expect looks something like: 我期望的表如下所示:

id    Project    Element    FloorId

Where FloorId comes from table2. 其中FloorId来自table2。

EDIT 编辑

Both tables contains "Project" and "Element" So, i can use them to match. 这两个表都包含“项目”和“元素”,因此,我可以使用它们进行匹配。 In table1 there is only one row per Project-Element but in table2 there can be multiple rows with Project-Element, so from table2 it's ok to only select the first match found. 在表1中,每个项目元素只有一行,但是在表2中,项目元素可以有多行,因此从表2中可以选择仅找到找到的第一个匹配项。

You can try below way - 您可以尝试以下方式-

SELECT a.id,a.project,a.element,b.floorid FROM dbo.IMP_ELEMENT a
            INNER JOIN dbo.IMP_MODEL_GEOMETRY b ON a.Project = b.Project and a.element=b.element
WHERE a.Project LIKE '%$objNr%'

I am new to SQL and don't really know what the "." 我是SQL的新手,并不真正知道“。”是什么。 stands for in "dbo.IMP". 在“ dbo.IMP”中代表。

The "." “。” is part of the syntax to identify the element you are after. 是识别所要元素的语法的一部分。 In full this is .. eg Adventureworks.dbo.customer 完全是..例如Adventureworks.dbo.customer

This extends to columns as well by adding another "." 通过添加另一个“”,这也扩展到了列。 and specifying the column name. 并指定列名称。

But developers are lazy, and if you do not specify the database then the engine uses the database the query is currently connected to. 但是开发人员很懒,如果您不指定数据库,那么引擎将使用查询当前连接到的数据库。 It is totally normal to not specify a database. 不指定数据库是完全正常的。

Actually we are really lazy and most don't specify the schema (the dbo. part) as the database engine will just check all schema. 实际上,我们真的很懒,大多数人都不指定架构(dbo。部分),因为数据库引擎只会检查所有架构。

Selecting data - joins 选择数据-联接

SELECT
    --use table aliases (t1.) for each column, this is shorthand for dbo.table1 
    --if the same column exists in more than one table the database engine knows 
    --which one you want by looking at the aliases  
    t1.column_name 
    ,t2.column_name
FROM 
    dbo.table1 as t1
    INNER JOIN dbo.table2 as t2  
        --inner join returns only rows where the join matches both tables 
        ON t1.primary_key_column = t2.foreign_key_column 
WHERE
    --the join gives you a super table with all the rows that matched the join
    --now you can add in any filters to your results
    t1.some_column = 'foo'
;

What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN? INNER JOIN,LEFT JOIN,RIGHT JOIN和FULL JOIN有什么区别?

Or you can try the below, 或者您可以尝试以下方法,

select table1.*, table2.FloorId
from IMP_ELEMENT as table1,
     IMP_MODEL_GEOMETRY as table2 
where table1.Project = table2.Project
  and table1.Project like '%Whatever%';

You can change the mail table as whatever you like. 您可以根据需要更改邮件表。

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

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