简体   繁体   English

MySQL-选择table1.something,table2。*替换所有列,如%something%

[英]Mysql - select table1.something, table2.* WHERE replace all columns like %something%

I have following query: 我有以下查询:

SELECT `mmetal`.`id`, `mmetal`.`name`, `steelmarks`.`EN`, `steelmarks`.`DIN` FROM `mmetal` LEFT JOIN `steelmarks` ON `mmetal`.`id`=`steelmarks`.`id` WHERE REPLACE(REPLACE(REPLACE(REPLACE(`name`,' ',''),'\)',''),'\(',''),'-','') LIKE '%something%'

(REPLACE - replacing of " ", "(", ")", "-" in name column) (替换-在名称栏中替换“”,“((”,“)”,“-”))

1) steelmarks table have about 15 columns - I need to replace 1)steelmarks表大约有15列-我需要替换

`mmetal`.`id`, `mmetal`.`name`, `steelmarks`.`EN`, `steelmarks`.`DIN`,`steelmarks`.`column3`,...,...,...`

with something like 用类似的东西

`mmetal`.`id`, `mmetal`.`name`, `steelmarks`.*

but it not works 但它不起作用

2) I wish to use LIKE function with REPLACE to all selected columns except id columns in both tables, not only "name". 2)我希望将LIKE函数和REPLACE一起用于两个表中id之外的 所有选定列,而不仅仅是“名称”。 Something like: 就像是:

WHERE REPLACE(REPLACE(REPLACE(REPLACE(ALL COLUMNS,' ',''),'\)',''),'\(',''),'-','') LIKE '%something%'

now i need to use 现在我需要使用

WHERE REPLACE...column1 LIKE %something% OR REPLACE...column2 LIKE %something% OR REPLACE...column3 LIKE %something% OR ...

Do you have any suggestion for my questions, please? 请问我的问题有什么建议吗?

The replace and like operators only operate on one column at a time. replace和like运算符一次只能在一个列上进行操作。 The following might do what you want, but you still have to list all the columns: 以下可能会做您想要的,但是您仍然必须列出所有列:

REPLACE(REPLACE(REPLACE(REPLACE(concat_ws('|', col1, col2, col3, . . . ),' ',''),'\)',''),'\(',''),'-','') LIKE '%something%'

This concatenates the values together with a separator. 这会将值与分隔符连接在一起。

Alternatively, you could query INFORMATION_SCHEMA.COLUMNS with something like: 或者,您可以使用以下内容查询INFORMATION_SCHEMA.COLUMNS

select concat(replace(<YOUR EXPRESSION HERE>, 'col1', c.column_name), ' and')
from information_schema.columns c
where table_name = YOURTABLEHERE

Then use the results to construct your query. 然后使用结果构建查询。

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

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