简体   繁体   中英

How to force datatype boolean in mysql view

CREATE VIEW as SELECT... FROM mytable

This creates a view with a column int unsiged .

But what I want is a boolean or tinyint column datatype in my view.

Is that possible, because neither CAST() nor CONVERT() seem to support casting to a boolean type.

If I create a normal table, the following results in a tinyint(1) :

CREATE TABLE test (
  Id        int(10)      unsigned NOT NULL auto_increment,
  myflag    boolean      default false,
  PRIMARY KEY (Id)
);

Can't I achieve the same in a view, means explicit setting a columns type to tinyint (1) ?

MySQL views do return TINYINT, but only if you query a column of that type. All other integer expressions return BIGINT.

I ran mysql --column-type-info to get debugging info on the results.

mysql> create table t ( b bool );

mysql> create or replace view v as select b from t;

mysql> select b from v;
Field   1:  `b`
Catalog:    `def`
Database:   `test2`
Table:      `v`
Org_table:  `v`
Type:       TINY
Collation:  binary (63)
Length:     1
Max_length: 0
Decimals:   0
Flags:      NUM 

mysql> select NOT b from v;
Field   1:  `NOT b`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       LONGLONG
Collation:  binary (63)
Length:     1
Max_length: 0
Decimals:   0
Flags:      BINARY NUM 

mysql> create or replace view v (b) as select NOT b from t;

mysql> select b from v;
Field   1:  `b`
Catalog:    `def`
Database:   ``
Table:      `v`
Org_table:  `v`
Type:       LONGLONG
Collation:  binary (63)
Length:     1
Max_length: 0
Decimals:   0
Flags:      BINARY NUM 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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