MySQL: Directive for all columns to run through substring?

冷暖自知 提交于 2019-12-24 23:28:40

问题


I am working with a MySQL database on the CLI which has a few rows that each contain hundreds of characters. I therefore use substring(columnName,1,20) on each one for each query. This is tedious! Is there any directive that I can give so that all queries will run the columns through substring?

Thanks!


回答1:


CREATE VIEW TableStripped AS
SELECT AcolumnThatDoesntNeedStripping
     , BcolumnThatDoesntNeedStripping
     , ...
     , SUBSTRING(columnA, 1, 20)
     , SUBSTRING(columnB, 1, 20)
     , SUBSTRING(columnC, 1, 20)
     , ...
     , SUBSTRING(columnZ, 1, 20)

and then use that view:

SELECT *
FROM TableStripped 



回答2:


I'd create a view for that.

For example, if you have table

CREATE TABLE t1 (long_column varchar(8000));

I'd create view

CREATE VIEW v1 AS SELECT SUBSTRING(long_column, 1, 20) AS long_column FROM t1;

And then rewrite all queries to use v1.long_column instead of SUBSTRING(t1.long_column, 1, 20).




回答3:


You can create a view that adds a calculation of substring (1,20) to the list of columns, and then use that calculated column in query expressions.



来源:https://stackoverflow.com/questions/8687970/mysql-directive-for-all-columns-to-run-through-substring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!