简体   繁体   English

SQL从一个表中选择项目,从另一个表中选择条件

[英]SQL select items from one table with conditions from another

I have 2 Tables: 我有2张桌子:

Accounts(ID, Name, Adress, Type)

Holidays(Name, StartDate, EndDate)

And the Type column from Accounts is a SET("Manager","Director","Administrator") for example. 例如SET("Manager","Director","Administrator")类型”列是SET("Manager","Director","Administrator")

The Name Column for both colums are relationed. 两个列的“名称”列是相关的。 When someone creates a holiday, the Name from table Holidays will be his name from the account that is logged in. 当某人创建假期时,“假期”表中的“名称”将是他在登录帐户中的名字。

And i want to know if it's possible to make a SQL statement where i can take all of table 2 which the the table1.type need to be "Director" and the Table1.name needs to be on holidays. 而且我想知道是否有可能制作一条SQL语句,在其中我可以将table2.type需要为“ Director”且Table1.name需要放假的所有表2都放入。

SELECT * FROM Accounts as a, Holidays as h WHERE a.Name = h.Name AND a.Type = 'Director' 

This should get you all the entries you want. 这将为您提供所有想要的条目。 It's important to link a.Name to h.Name to ensure that you get the right combination. a.Name链接到h.Name以确保获得正确的组合。

I think this will make the trick. 我认为这可以解决问题。 You don't need to check for the name in Holidays because when we are making the left join we say take the records where the name is equal on both tables. 您不需要在Holidays中检查名称,因为当我们进行左联接时,我们说的是记录两个表上名称相等的记录。 If it is not in holidays you are not going to get any records. 如果不是节假日,则不会获得任何记录。

select Accounts.ID,Accounts.Name,Accounts.Adress,Accounts.Type,Holidays.Name,Holidays.StartDate,Holidays.EndDate from Accounts left join Holidays on Accounts.Name=Holidays.Name where Accounts.Type = "Director";

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

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