问题
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