SQL在两个表上加入并替换和返回列
我正在使用JONINS进行一些SQL查询,并想从与原始表列值匹配的另一个表中获取相应的值。
例如,我有两个名为产品
和类别
的不同表格,它们如下:
产品:
| id | name | category1 | category2 | category3 |
| -- | ------- | ------------------------- |--------------------------- |
| 1 | T-shirt | Cloths&Accessories~:/ID1 | Cloths~:/ID2 | Shirts~:/ID3|
|----|---------|---------------------------|------------- |-------------|
| 2 | T-shirt | Cloths&Accessories~:/ID1 | Cloths~:/ID2 | Shirts~:/ID3|
|----|---------|---------------------------|------------- |-------------|
| 3 | T-shirt | Cloths&Accessories~:/ID1 | Cloths~:/ID2 | Shirts~:/ID3|
|----|---------|---------------------------|------------- |-------------|
| 4 | T-shirt | Cloths&Accessories~:/ID1 | Cloths~:/ID2 | Shirts~:/ID3|
|----|---------|---------------------------|------------- |-------------|
| 5 | T-shirt | Cloths&Accessories~:/ID1 | Cloths~:/ID2 | Shirts~:/ID3|
|----|---------|---------------------------|------------- |-------------|
和类别:
| id | categories |
| -- | -------------------- |
| 1 | Cloths & Accessories |
|----|--------------------- |
| 2 | Cloths |
|----|--------------------- |
| 3 | Shirts |
|----|--------------------- |
问题是,在表product
中有此blots& cockesore〜:/id1
一种存储在列名称下的无效字符串category1
, category2
和category3
,我想从表类别
中替换为有效的类别名称。如果您查看表类别
每个类别的ID与字符串botbers& cossecories& costausie〜:/id1
在其中id1
。
例如: 布置&附件〜:/ID1
在此字符串id1
中与表category
属于bloths&附件
我必须替换并返回所有category1
,category2
和category3
table code> product> product 的列使用有效的类别
。
为此,最佳的SQL加入查询是什么?
I am working on some SQL query with joins and I want to get the corresponding value from another table which matches with original table columns values.
For example, I have two different tables named Product
and Category
and they are as follows:
Products:
| id | name | category1 | category2 | category3 |
| -- | ------- | ------------------------- |--------------------------- |
| 1 | T-shirt | Cloths&Accessories~:/ID1 | Cloths~:/ID2 | Shirts~:/ID3|
|----|---------|---------------------------|------------- |-------------|
| 2 | T-shirt | Cloths&Accessories~:/ID1 | Cloths~:/ID2 | Shirts~:/ID3|
|----|---------|---------------------------|------------- |-------------|
| 3 | T-shirt | Cloths&Accessories~:/ID1 | Cloths~:/ID2 | Shirts~:/ID3|
|----|---------|---------------------------|------------- |-------------|
| 4 | T-shirt | Cloths&Accessories~:/ID1 | Cloths~:/ID2 | Shirts~:/ID3|
|----|---------|---------------------------|------------- |-------------|
| 5 | T-shirt | Cloths&Accessories~:/ID1 | Cloths~:/ID2 | Shirts~:/ID3|
|----|---------|---------------------------|------------- |-------------|
And Category:
| id | categories |
| -- | -------------------- |
| 1 | Cloths & Accessories |
|----|--------------------- |
| 2 | Cloths |
|----|--------------------- |
| 3 | Shirts |
|----|--------------------- |
The problem is, in table Product
there is this Cloths&Accessories~:/ID1
kind of Invalid strings stored under column names category1
, category2
and category3
and I want to replace them with valid category names from table Category
. If you look at the table Category
the id of each category matches with string Cloths&Accessories~:/ID1
having ID1
in it.
For example:Cloths&Accessories~:/ID1
in this string ID1
is related to id=1 of table Category
which is Cloths & Accessories
I have to replace and return all of category1
, category2
and category3
columns of table Product
with valid categories
.
What would be the optimal SQL join query for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 提取ID,然后加入它:
You could use
substring_index
to extract the ID, and then join on it: