无法获取 SQL 语句来更新 2 个表?
我目前有一个数据库,其中有两个表,一个表包含工作详细信息和其他公司详细信息。 “company_name”存在于“Jobs”表中,该表将在“Companies”表中的“name”字段下有一个匹配条目。我想基本上将“comp_id”的“Companies”表中的字段设置为“Jobs”表中的字段“id”的值,其中“Companies”表中的“name”等于“ “职位”表中的“公司名称”。
我已经创建了下面的查询,我相信它应该可以工作,但是它返回没有受影响的行?谁能帮我解决这个问题吗?
UPDATE `jobs`, `companies`
SET `comp_id` = 'companies.id'
WHERE ('companies.name' = 'jobs.company_name')
谢谢
I currently have a database with two tables in it one hold lets say job details and the other company details. The "company_name" exists in the "Jobs" table which will have a matching entry in the "Companies" table under the field of "name". I want to basically set the field in the "Companies" table of "comp_id" to be the value of the field "id" in the "Jobs" table, WHERE the "name" in the "Companies" table is equals to the "company_name" in the "Jobs" table.
I have create the query below which i believed should work however it returns no rows affected?? can anyone please help me with this?
UPDATE `jobs`, `companies`
SET `comp_id` = 'companies.id'
WHERE ('companies.name' = 'jobs.company_name')
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这种情况:
WHERE ('companies.name' = 'jobs.company_name')
是一个问题(除非这是发布过程中的复制和粘贴错误)
您正在比较两个字符串文字(以及当然,因为它们不一样,你永远不会更新任何东西)。
原因是您使用单引号表示字符串文字。要引用列名,您需要使用双引号或之前使用过的反引号(假设您使用的是 MySQL)。
This condition:
WHERE ('companies.name' = 'jobs.company_name')
is one problem (unless this is a copy & paste error during posting)
You are comparing two string literals there (and of course as they are not the same, you will never update anything).
The reason is you are using single quotes which denote a string literal. To quote column names you either need to use double quotes or the backticks you had used before (assuming you are on MySQL).
阅读您的问题,您只更新一张表而不是两张表?
您所需要做的就是将
companies
表中的comp_id
列设置为jobs
中id
列的值code> 表中的name
和company_name
列相同?要使用 MSSQL 执行此操作,您需要执行以下操作:
Reading your question you are only updating one table not two?
All you want to do is set the
comp_id
column in thecompanies
table to the value of theid
column in thejobs
table where thename
andcompany_name
columns are the same?To do this using MSSQL you would do something like this:
在 SQL Server 中,您只能在 UPDATE 语句中更新 1 个表。要一次更新多个表,您有以下选项:
使用存储过程
创建 select 语句的视图并从视图中删除
使用触发器。
You can update only 1 table in an UPDATE statement in SQL Server. To update multiple tables at once you have the following options:
Use Stored procs
Create a View of the select statement and delete from the view
Use triggers.