我的道歉 - 我意识到这是一个非常常见的问题,但是我在查看需要使用的查询时遇到了一些麻烦。
我基本上是在尝试将值从一个表复制到另一个表,基于查询第二个表。
例如,我有两个表:
+-----------------------------------------------------+
| Table 1 |
+----------+------+----------+------------+-----------+
| UniqueID | name | location | Flavour | Status |
| 723948 | | | Mango | Tried |
| 723948 | | | Orange | Not tried |
| 723948 | | | Strawberry | Tried |
| 2346 | | | Mango | Not tried |
| 2346 | | | Strawberry | Tried |
| 3745 | | | Strawberry | Tried |
| 3745 | | | Mango | Tried |
+----------+------+----------+------------+-----------+
和
+-------------------------------------------+
| Table 2 |
+----------+-----------------+--------------+
| UniqueID | fullname | baselocation |
| 723948 | Steve Stevenson | London |
| 2346 | Mary Marington | New York |
+----------+-----------------+--------------+
然后我希望根据与表2匹配的UniqueID来填充表1中的名称和位置列,因此结果如下:
+--------------------------------------------------------------------------------+
| Desired result (Table 1) |
+--------------------------+-----------------+----------+------------+-----------+
| UniqueID | name | location | Flavour | Status |
| 723948 | Steve Stevenson | London | Mango | Tried |
| 723948 | Steve Stevenson | London | Orange | Not tried |
| 723948 | Steve Stevenson | London | Strawberry | Tried |
| 2346 | Mary Marington | New York | Mango | Not tried |
| 2346 | Mary Marington | New York | Strawberry | Tried |
| 3745 | | | Strawberry | Tried |
| 3745 | | | Mango | Tried |
+--------------------------+-----------------+----------+------------+-----------+
我知道我应该使用UPDATE,SET,FROM然后加入的内容,但我不能100%确定正确,最准确/最有效的查询是什么。
谢谢!
答案 0 :(得分:2)
您正在使用MySQL's multi-table UPDATE
syntax寻找带有相当传统const paths = {
src: './src',
publicSrc: './public/js',
dest: './app',
bundle: 'bundle.js',
bundleDest: './app/public/js',
publicEntries: [
'./public/js/index',
'./public/js/components/test' <--- Remove this line.
]
};
的{{1}}语句。
基本格式为:
UPDATE
在你的情况下:
INNER JOIN
只会修改匹配的行,这就是为什么您可以使用UPDATE
T1
JOIN T2 ON T1.col = T2.col
JOIN T3 ON T2.other = T3.other
...
SET
T1.updatedcol = T2.colvalue,
T1.updatedcol2 = T2.colvalue2,
执行此操作而不是UPDATE
Table1
INNER JOIN Table2 ON Table1.UniqueID = Table2.UniqueID
SET
-- Set values in Table1 from joined rows in Table2
Table1.name = Table2.fullname,
Table1.location = Table2.baselocation
。
(Here is a demonstration,虽然SQLfiddle目前无法运行INNER JOIN
)
答案 1 :(得分:0)
尝试使用以下查询
UPDATE `table 1` AS t1
INNER JOIN `table 2` AS t2
ON t1.UniqueID = t2.UniqueID
SET
name = fullname,
location=baselocation