简体   繁体   English

在MySQL select的另一个表中用列的值替换列

[英]replace a column with a column's value in another table in MySQL select

I have 2 tables in a database with these sample data: 我在数据库中有2个表,其中包含以下示例数据:

listings table: 列表表:

listings.locations_L1 : 'province_far_shiraz' listings.locations_L1 :“ province_far_shiraz”

lang_keys table: lang_keys表:

lang_keys.Key : 'data_formats+name+province_far_shiraz' lang_keys.Key :“ data_formats +名称+ province_far_shiraz”

lang_keys.Value : 'Shiraz City' lang_keys.Value :“设拉子市”


I want to select from table listings and replace values of column listings.locations_L1 with lang_keys.Value in result. 我想从表listings进行选择,并在结果中用lang_keys.Value替换列listings.locations_L1值。

Note that string data_formats+name+ is concatinated to all records of table lang_keys, column Value . 请注意,字符串data_formats+name+被隐含在表lang_keys的Value列的所有记录中。

Can you help please? 你能帮忙吗?

One way to achieve it would be to join the tables based on comparing to Key to a concatenation data_formats+name+ to listings . 一种实现方法是将表与Key进行比较,然后将data_formats+name+listings进行比较,从而将表连接data_formats+name+ locations_L1 in the following way: 按照以下方式进行locations_L1

SELECT * FROM `listings`
INNER JOIN `lang_keys` ON 
  `lang_keys`.`Key` = CONCAT('data_formats+name+', `listings`.`locations_L1`)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM