我的数据库中有以下表格:
表格顺序
|------------------------------------------------------|
| Id | uid | order | price | City_to_be_update
|------------------------------------------------------|
| 1 | 25 | someFoods | 10025 |
|------------------------------------------------------|
| 2 | 30 |Some veggies| 2015|
|------------------------------------------------------|
| 3 | 12 |Milk | 145 |
|------------------------------------------------------|
表用户:
|------------------------------------------------|
| Id | uid | city_id | Address|
|------------------------------------------------|
| 1 | 30 | 1 | myAddress |
|------------------------------------------------|
| 2 | 12 | 2 | Asdfads |
|------------------------------------------------|
| .... | .. | .. | ....... |
|------------------------------------------------|
表城市:
|------------------------------------------------|
| Id |city_id| city_name | Country|
|------------------------------------------------|
| 1 | 2 | Abc | Uae |
|------------------------------------------------|
| 2 | 3 | Def | Ghi |
|------------------------------------------------|
| .... | .. | .. | ....... |
|------------------------------------------------|
我想做的是
Select * from `order`
$user_id [ uid ];
Select * from `users` where `uid` = ‘$user_id’;
$city_id [ city_id ];
从city
中选择*,其中city_id
= ‘$city_id’;
$city_name [ city_name ];
最后要更新订单表如下
Update `order` SET ` City_to_be_update`= $city_name’;
如何做到这一点,请有人帮助我吗?
最佳答案
尝试使用连接查询,
UPDATE
< OrderTable > AS a LEFT JOIN < UsersTable > AS b ON a.uid=b.uid
LEFT JOIN < CityTable > AS c ON b.city_id=c.city_id
SET
a.City_to_be_update=c.city_name
关于php - 如何在PHP中基于另一个表更新mysql表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34174872/