且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Sql Server中编写嵌套查询/子查询

更新时间:2021-09-05 02:23:29

听起来您需要在两个客户端表之间进行连接然后将信息插入第三个表格。



您可以使用类似

插入到Birthday_Client(fieldname 1,fieldname2等)中选择fieldname 1 ,db-1内部连接数据库中的dbname 1,dbname 2 = db-2.ClientID,db-2.DOB = getdate()





您可能需要注意dob字段。如果它包含时间,那么您可能需要扩展查询,使其在当天的开始和结束之间。
Sounds like you need a join between the two client tables and then insert the information into a third table.

You can use something like
insert into Birthday_Client (fieldname 1, fieldname2 etc) select fieldname 1, fieldname2 etc from db-1 inner join db-2 on db-1.ClientID = db-2.ClientID where db-2.DOB = getdate()


You may need to be careful about the dob field. If it includes time then you may need to expand the query so that it is between the start and end of the day.


You can use something like

insert into Clients_Birthday
(clientId) select db-1.ClientIDfrom db-1 inner join db-2 on db-1.ClientID = db-2.ClientID
 where  DATEPART(d, db-2.DOB= DATEPART(d, GETDATE())
    AND DATEPART(m, db-2.DOB= DATEPART(m, GETDATE())