简体   繁体   English

编辑:问题已更改 - SQL:从表中的所有行获取列名称不在另一个表中的行

[英]Edit: QUESTION CHANGED - SQL: Taking all rows from table where column names are NOT in another table

Never done SQL before (how that's possible I don't know), 从来没有做过SQL(我怎么可能不知道),

I have two tables, like so: 我有两张桌子,如下:

Table1:

Column Names:    A    B    C
Rows:            1  sdf  sdsd
                 2  seg  werr

and

Table2:

Column Names:    A    B    C     D    E    F
Rows:            1    sdf  sdsd  yuj  uui  ddd
                 1    sdf  sdsd  sss  sdd  ssw
                 1    sdf  sdsd  jut  scv  sef
                 2    seg  werr  oel  ewe  wee
                 2    seg  werr  ujf  etr  wuk
                 2    seg  werr  los  hjd  wee

EDIT: Question changed. 编辑:问题已更改。

How do I take all rows with the columns that are unique in the 2nd table? 如何使用第二个表中唯一的列获取所有行? Ie I only want the data from D, E and F where the values in A, B, C in Table1 correspond to the A, B, C values in Table2. 即我只想要来自D,E和F的数据,其中表1中A,B,C中的值对应于表2中的A,B,C值。

So for instance given A=1 and B=sdf, I want the rows: 所以例如给定A = 1和B = sdf,我想要行:

sdsd  yuj  uui  ddd
sdsd  sss  sdd  ssw
sdsd  jut  scv  sef

I could just go: SELECT * FROM Table2 WHERE A='1' AND B='sdf' but I would get 我可以去:SELECT * FROM Table2 WHERE A ='1'AND B ='sdf'但我会得到

1    sdf  sdsd  yuj  uui  ddd
1    sdf  sdsd  sss  sdd  ssw
1    sdf  sdsd  jut  scv  sef

The most direct way would be: 最直接的方式是:

Select D, E From Table2

If you are looking for something dynamic, the solution will depend on which database vendor you are using. 如果您正在寻找动态的东西,解决方案将取决于您使用的是哪个数据库供应商。 There is not an agnostic dynamic solution. 没有不可知的动态解决方案。

Just list columns you want to select. 只需列出要选择的列。

So for instance given A='1' and B='sdf' : 例如,给定A='1'B='sdf'

SELECT C,D,E,F FROM Table2 WHERE A='1' AND B='sdf' 

You want a LEFT JOIN (or RIGHT JOIN ) with a special operation in the WHERE to look for non-matching values. 您希望在WHERE使用特殊操作进行LEFT JOIN (或RIGHT JOIN )以查找不匹配的值。

In this case, something like this: 在这种情况下,这样的事情:

SELECT Table2.*
FROM Table2 LEFT JOIN Table1 USING (SomeCommonField)
WHERE Table1.SomeCommonField IS NULL

Generally, You need to access the system tables or the catalog views to do this. 通常,您需要访问系统表或目录视图才能执行此操作。 The exact syntax would depend on what DBMS you are using... 确切的语法取决于您使用的DBMS ...

It seems that here you have one of the only cases where a NATURAL JOIN is the proper solution since your column names are exactly the same in both tables. 在这里,您似乎只有一种情况,其中NATURAL JOIN是正确的解决方案,因为您的列名在两个表中完全相同。

 SELECT D, E, F
   FROM table1
NATURAL JOIN table2
  WHERE a = '1'
    AND b = 'sdf'
    AND c = 'sdsd'

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

相关问题 如何 select 列名来自 SQL 中另一个表的值? - How to select values where column names are from another table in SQL? 从SQL中的另一个表中获取列名 - Fetching column names from another table in SQL 从表中选择所有表名来自 SQL 中另一个表的表 - Select all from tables where table names are from another table in SQL 获取一个表的所有行,并从另一个表中匹配行,其中SQL中可能不存在条目 - Getting all rows of one table, and matching rows from another table where entries may not exist in SQL SQL从父表返回条件与子表的另一个子表匹配的所有行 - Sql return all rows from parent table where criteria match from another child of a child table SQL选择链接表中所有行在x列中具有相同值的行 - SQL Select rows where all rows from linked table have the same value in column x 在SQL Server中,如何使用另一个表中的列名比较行 - In SQL Server, how can I compare rows using the column names from another table sql 连接未从另一个表中获取所有记录 - sql join not taking all records from another table SQL - 一个表的行是另一个表的列标题的位置 - SQL - Pivoting where the rows of one table are the column titles of another SQL 查询从另一个表更新表中的列(多行) - SQL query to update a column(multiple rows) in a table from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM