简体   繁体   English

将“ INNER JOIN”查询从Access移到C#和SQL

[英]Moving “INNER JOIN” Query from Access to C# and SQL

I'm having hard time with my Access query being run via C# code and SQL. 我很难通过C#代码和SQL运行Access查询。

I'm getting error saying something about 'INNER'. 我在说“内在”时出错。

UPDATE Service INNER JOIN Sales ON ([Service].VIN=Sales.VIN) AND (Service.Address=Sales.address) SET Service.Selldate = Sales.Selldate
WHERE Service.Address=Sales.address And Service.VIN=Sales.VIN;

Access lets you write sql in that way but sql server wont you need to change your update statement so that the joins are outside the update Access允许您以这种方式编写sql,但是sql server不需要更改更新语句,以便联接不在更新之外

something along the lines of the following should work 遵循以下内容的某些东西应该起作用

UPDATE s
SET s.Selldate = sa.Selldate
from 
Service s INNER JOIN Sales sa ON s.VIN=sa.VIN AND s.Address=se.address
WHERE s.Address=se.address And s.VIN=sa.VIN;

You can use alias as ser forService and sale for Sales and build you updatestatement like this. 您可以使用别名作为ser forService和sales进行销售,并像这样构建updatestatement。

UPDATE ser SET ser.Selldate = sale.Selldate  
FROM Service ser    
JOIN Sales sale ON (ser.VIN=sale.VIN) AND (ser.Address=sale.address) 
WHERE ser.Address=sale.address And ser.VIN=sale.VIN;

Consider using a view in your DBMS, then all you have to do is use the view name as the table name. 考虑在DBMS中使用视图,那么您所要做的就是使用视图名称作为表名称。

In MSSQL you can create views which are effectively saved SELECT statements which you can call like tables. 在MSSQL中,您可以创建视图,这些视图是有效保存的SELECT语句,可以像表一样调用它们。 Open you DBMS right-click on views and add new view. 右键单击视图打开DBMS,然后添加新视图。 Think of a view like a query in access. 将视图视为访问中的查询。

I think you're better off doing this as two different queries, but if you wanted to do this in one go, you could use subqueries. 我认为您最好将其作为两个不同的查询来执行,但是如果您想一次性执行此操作,则可以使用子查询。

UPDATE Service SET Service.Selldate = Sales.Selldate
WHERE service.vin in (select sales.vin from sales) and service.address in (select sales.vin from sales) 

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

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