Mysql查看条件相似

时间:2011-12-24 14:14:36

标签: mysql

我有一个数据库,可以保留一些人的信息。

我发送以下查询:

(select 'NUMBER OF BRAND PARTNERS ACTIVE PROMOTERS' AS `NUMBER OF ACTIVE PROMOTERS`,count(*) AS `COUNT( * )` from `Registration_Summary` where promoter not like "bw%")

并且它完美地运行,但每当我创建一个视图来保存此命令时,“启动器不喜欢”bw%“”部分完全被忽略,为什么?!这么奇怪 ?!好像视图不能有“喜欢”的条件!!

我的查看命令是:

CREATE VIEW test AS
(select 'NUMBER OF BRAND PARTNERS ACTIVE PROMOTERS' AS `NUMBER OF ACTIVE PROMOTERS`,count(*) AS `COUNT( * )` from `Registration_Summary` where promoter not like "bw%")

我认为我的问题应该是如何基于另一个带有“where like”语句的视图创建视图?

3 个答案:

答案 0 :(得分:2)

我认为其他可能会出错。复制你的问题,如:

create table `Registration_Summary` (
    id integer primary key, 
    promoter varchar(200)
);

insert into `Registration_Summary` (id, promoter) values (1, 'bar1');
insert into `Registration_Summary` (id, promoter) values (2, 'bar2');
insert into `Registration_Summary` (id, promoter) values (3, 'bar3');
insert into `Registration_Summary` (id, promoter) values (4, 'foo1');
insert into `Registration_Summary` (id, promoter) values (5, 'foo1');
insert into `Registration_Summary` (id, promoter) values (6, 'foo1');
insert into `Registration_Summary` (id, promoter) values (7, 'foo1');
insert into `Registration_Summary` (id, promoter) values (8, 'afoo1');
insert into `Registration_Summary` (id, promoter) values (9, 'bfoo1');
insert into `Registration_Summary` (id, promoter) values (10, 'cfoo1');

从注册表中选择

select * from `Registration_Summary` where promoter like 'foo%';

将产生以下输出

+----+----------+
| id | promoter |
+----+----------+
|  4 | foo1     |
|  5 | foo1     |
|  6 | foo1     |
|  7 | foo1     |
+----+----------+

select * from `Registration_Summary` where promoter not like 'foo%';

将产生以下内容

+----+----------+
| id | promoter |
+----+----------+
|  1 | bar1     |
|  2 | bar2     |
|  3 | bar3     |
|  8 | afoo1    |
|  9 | bfoo1    |
| 10 | cfoo1    |
+----+----------+

所以创建像你一样的视图

create or replace view testview as (select 'NUMBER OF BRAND PARTNERS ACTIVE PROMOTERS' AS `NUMBER OF ACTIVE PROMOTERS`,count(*) AS `COUNT( * )`  from `Registration_Summary` where promoter not like 'foo%');

然后从视图中选择

SELECT * FROM testview;

生成正确的输出

+-------------------------------------------+------------+
| NUMBER OF ACTIVE PROMOTERS                | COUNT( * ) |
+-------------------------------------------+------------+
| NUMBER OF BRAND PARTNERS ACTIVE PROMOTERS |          6 |
+-------------------------------------------+------------+

答案 1 :(得分:0)

我想我知道这是什么问题。我在另一个视图而不是表格上发布where条件,我认为这就是我没有得到正确答案的原因。我是对的吗?

我仍然没有解决问题,但是当我使用表创建一个select命令的视图时,它可以工作,但是当我使用它来从另一个视图中选择时,它在使用where语句时给出了错误的答案。

答案 2 :(得分:0)

回复旧线程,因为我刚遇到了同样的问题。

DROP TABLE IF EXISTS like_test;

CREATE TABLE like_test (
     id int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Unique ID'
    ,textvalue varchar(64) COLLATE ucs2_bin NOT NULL
    ,PRIMARY KEY (id)
) ENGINE=InnoDB CHARSET=ucs2 COLLATE=ucs2_bin;

INSERT INTO like_test (textvalue)
VALUES
 ('foofoo')
,('foobar')
,('barfoo')
,('barbar');

DROP VIEW IF EXISTS view_like_test;

CREATE VIEW view_like_test AS
SELECT
     id
    ,textvalue
FROM like_test
WHERE textvalue NOT LIKE 'foo%';

SHOW CREATE VIEW view_like_test;

在MySQL的内部修改之后,视图已经剥离了LIKE条件:

CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `symbols_mgr`@`%` 
    SQL SECURITY DEFINER
VIEW `view_like_test` AS
    select 
        `like_test`.`id` AS `id`,
        `like_test`.`textvalue` AS `textvalue`
    from
        `like_test`
    where
        (not ((`like_test`.`textvalue` like '')))

charset / collat​​ion导致LIKE条件被清空 - 因此OP可能具有类似的表结构。

我使用的是5.1版本 - 从那时起,这可能已经不再成为问题了。快速继续5.5并且LIKE条件改为:

(not ((`like_test`.`textvalue` like '\0\0\0f\0\0\0o\0\0\0o\0\0\0%')))

如果它可行,那么为MySQL提供更多数据(例如latin / ascii)可能是解决问题的最简单方法。或者您可以像这样更改视图:

(not ((`like_test`.`textvalue` like 'foo%' collate utf8_bin)))

...您指定了排序规则。