mysql 递归更新

发布于 2024-07-08 01:02:56 字数 304 浏览 9 评论 0原文

我有两个 mysql 表,一个包含汽车的详细信息,另一个包含汽车的所有可能型号,如下所示:

cars:
  car_id
  model
  [more details]

models:
  model_id
  model_name

现在,我的问题是存储在“cars”表中的模型详细信息是 model_name,而不是 model_id就是我想要的,

我猜我需要某种递归更新表才能更新 cars.model,但我的大脑停止了工作,不知道该怎么做。 有人对如何做到这一点有任何提示吗?

感谢任何能提供帮助的人!

I have two mysql tables, one containing details on cars and one containing all the possible models of the cars, as such:

cars:
  car_id
  model
  [more details]

models:
  model_id
  model_name

Now, my problem is that the model details stored in the 'cars' table is the model_name, rather than the model_id which is what I want

I'm guessing I need some kind of recursive UPDATE table to be able to update cars.model, but my brain's stopped working and can't figure out how to do it. Does anyone have any hints on how this would be done?

Thanks to anyone who can help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

近箐 2024-07-15 01:02:56
mysql> create table cars(car_id int auto_increment primary key, model 
varchar(50), model_id int);
Query OK, 0 rows affected (0.01 sec)

mysql> create table models(model_id int auto_increment primary key, name 
varchar(50));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into models(name) VALUES ('Taurus'), ('Ka'), ('Mustang');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> insert into cars(model) VALUES ('Ka'), ('Mustang'), ('Taurus'), 
('F150');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from cars;
+--------+---------+----------+
| car_id | model   | model_id |
+--------+---------+----------+
|      1 | Ka      |     NULL |
|      2 | Mustang |     NULL |
|      3 | Taurus  |     NULL |
|      4 | F150    |     NULL |
+--------+---------+----------+
4 rows in set (0.00 sec)

mysql> select * from models;
+----------+---------+
| model_id | name    |
+----------+---------+
|        1 | Taurus  |
|        2 | Ka      |
|        3 | Mustang |
+----------+---------+
3 rows in set (0.00 sec)

mysql> update cars,models set cars.model_id = models.model_id 
where models.name = cars.model;
Query OK, 3 rows affected (0.06 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from cars;
+--------+---------+----------+
| car_id | model   | model_id |
+--------+---------+----------+
|      1 | Ka      |        2 |
|      2 | Mustang |        3 |
|      3 | Taurus  |        1 |
|      4 | F150    |     NULL |
+--------+---------+----------+
4 rows in set (0.03 sec)

mysql> create table cars(car_id int auto_increment primary key, model 
varchar(50), model_id int);
Query OK, 0 rows affected (0.01 sec)

mysql> create table models(model_id int auto_increment primary key, name 
varchar(50));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into models(name) VALUES ('Taurus'), ('Ka'), ('Mustang');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> insert into cars(model) VALUES ('Ka'), ('Mustang'), ('Taurus'), 
('F150');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from cars;
+--------+---------+----------+
| car_id | model   | model_id |
+--------+---------+----------+
|      1 | Ka      |     NULL |
|      2 | Mustang |     NULL |
|      3 | Taurus  |     NULL |
|      4 | F150    |     NULL |
+--------+---------+----------+
4 rows in set (0.00 sec)

mysql> select * from models;
+----------+---------+
| model_id | name    |
+----------+---------+
|        1 | Taurus  |
|        2 | Ka      |
|        3 | Mustang |
+----------+---------+
3 rows in set (0.00 sec)

mysql> update cars,models set cars.model_id = models.model_id 
where models.name = cars.model;
Query OK, 3 rows affected (0.06 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from cars;
+--------+---------+----------+
| car_id | model   | model_id |
+--------+---------+----------+
|      1 | Ka      |        2 |
|      2 | Mustang |        3 |
|      3 | Taurus  |        1 |
|      4 | F150    |     NULL |
+--------+---------+----------+
4 rows in set (0.03 sec)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文