[英]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.