【问题标题】:How can I replace within a SELECT the statement from one table to ther other one?如何在 SELECT 中将语句从一个表替换到另一个表?
【发布时间】:2019-09-26 22:19:00
【问题描述】:

我正在尝试从表 A 中选择两个表来替换表 B 中的值的属性。 对我来说重要的是表格没有更新。

table_A

id  category_id filename
1   2   apple
2   12  banana
3   453 pineapple

table_B

id  category_color  category_type
2   red fruit
12  yellow  fruit
453 brown   fruit

MYSQL 版本是 mysql Ver 14.14 Distrib 5.5.62,适用于使用 readline 6.3 的 debian-linux-gnu (x86_64)

mysql> SELECT * FROM table_A INNER JOIN table_B ON table_A.category_id = table_B.id SET table_A.category_id = category_name;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET table_A.category_id = category_name' at line 1

输出应该像下面的例子

输出

id  category_id filename
1   red apple
2   yellow  banana
3   brown   pineapple

【问题讨论】:

  • SET 是干什么用的???你是SELECTing 记录。

标签: mysql sql database join select


【解决方案1】:

SET 不属于 SELECT 查询。相反,请引用字段列表中第二个表中的值:

SELECT A.id, B.category_name, A.filename 
FROM table_A A 
INNER JOIN table_B B ON A.category_id = B.id

输出

id  filename    category_name
1   apple       red
2   banana      yellow
3   pineapple   brown

Demo on dbfiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    相关资源
    最近更新 更多