不等于 - MySQL明确没有和组

时间:2015-01-02 05:45:45

标签: mysql

我正在寻找可以列出"没有/或不等于值"的SQL语法,但是我的要求 应按名称对结果进行分组,并仅列出没有"没有/或不等于值的名称"

当我跑步时

SELECT * FROM promotions WHERE promo_name != 'barbie';

我得到以下结果:

d   Name            promo_name      Remarks
2   Male Adult      Anniversary     NULL
3   Female Adult    Valentines Day  NULL
4   Boy             Play Station    NULL
5   Old Guy         Retirement      NULL
7   Girl            Skirts          NULL
8   Girl            Chocolates      NULL
9   Girl            Christmas       NULL
10  Male Adule      Valentines Day  NULL
11  Male Adule      Christmas       NULL
12  Female Adult    Christmas       NULL
13  Boy             Christmas       NULL

我正在寻找一个生成以下结果的查询:条件= WHERE promo_name != 'barbie'

d   Name            promo_name      Remarks
2   Male Adult                      NULL
3   Female Adult                    NULL
4   Boy                             NULL
5   Old Guy                         NULL
6   Old Lady                        NULL

另一个例子

WHERE promo_name != 'Christmas';

寻找如下结果:

2   Male Adult                      NULL
5   Old Guy                         NULL
6   Old Lady                        NULL

我在下面附加sql dump。

CREATE TABLE `test`.`promotions` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `Name` VARCHAR(45) NULL,
  `promo_name` VARCHAR(45) NULL,
  `Remarks` VARCHAR(100) NULL,
  PRIMARY KEY (`id`));

USE test; 

insert into `promotions`(`id`,`Name`,`promo_name`,`Remarks`) values (1,'Girl','Barbie',' ');
insert into `promotions`(`id`,`Name`,`promo_name`,`Remarks`) values (2,'Male Adult','Anniversary',' ');
insert into `promotions`(`id`,`Name`,`promo_name`,`Remarks`) values (3,'Female Adult','Valentines Day',' ');
insert into `promotions`(`id`,`Name`,`promo_name`,`Remarks`) values (4,'Boy','Play Station',' ');
insert into `promotions`(`id`,`Name`,`promo_name`,`Remarks`) values (5,'Old Guy','Retirement',' ');
insert into `promotions`(`id`,`Name`,`promo_name`,`Remarks`) values (6,'Old Lady',null,'');
insert into `promotions`(`id`,`Name`,`promo_name`,`Remarks`) values (7,'Girl','Skirts',null);
insert into `promotions`(`id`,`Name`,`promo_name`,`Remarks`) values (8,'Girl','Chocolates',null);
insert into `promotions`(`id`,`Name`,`promo_name`,`Remarks`) values (9,'Girl ','Christmas',null);
insert into `promotions`(`id`,`Name`,`promo_name`,`Remarks`) values (10,'Male Adule','Valentines Day',null);
insert into `promotions`(`id`,`Name`,`promo_name`,`Remarks`) values (11,'Male Adule','Christmas',null);
insert into `promotions`(`id`,`Name`,`promo_name`,`Remarks`) values (12,'Female Adult','Christmas',null);
insert into `promotions`(`id`,`Name`,`promo_name`,`Remarks`) values (13,'Boy','Christmas',null);

1 个答案:

答案 0 :(得分:1)

从我的理解(完全没有得到你的问题,但似乎但是)你不想要promo_name列,所以如果你只想要两个列,请不要使用*

将他们的名字用作

select Name,     Remarks from promotions where promo_name!='barbie'