mysql从表中选择数据

时间:2015-07-14 20:15:47

标签: mysql

我有一个带有人员列和删除列的用户表。

我不想显示具有值为1

的删除列的用户
SELECT *
            FROM Users
            WHERE
            Users.staff = 1
            AND Users.remove != 1

我没有收到错误,但我的代码不起作用。

这是架构

    CREATE TABLE `Users` (
  `userId` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `fullName` varchar(50) DEFAULT NULL,
  `firstName` varchar(25) NOT NULL DEFAULT '',
  `lastName` varchar(25) NOT NULL DEFAULT '',
  `address` varchar(50) NOT NULL DEFAULT '',
  `city` varchar(25) DEFAULT NULL,
  `state` char(2) DEFAULT NULL,
  `zipCode` varchar(25) DEFAULT NULL,
  `email` varchar(50) NOT NULL DEFAULT '',
  `cellPhone` varchar(15) DEFAULT NULL,
  `birthDate` date NOT NULL,
  `creditCard` varchar(250) NOT NULL DEFAULT '',
  `subscriptionStarted` date NOT NULL,
  `subscriptionEnded` date NOT NULL,
  `basicPlan` tinyint(1) DEFAULT NULL,
  `standardPlan` tinyint(1) DEFAULT NULL,
  `premiumPlan` tinyint(1) DEFAULT NULL,
  `remove` tinyint(1) DEFAULT NULL,
  `staff` tinyint(1) DEFAULT NULL,
  `admin` tinyint(1) DEFAULT NULL,
  `systemAdmin` tinyint(1) DEFAULT NULL,
  `edited` datetime DEFAULT NULL,
  `created` datetime NOT NULL,
  PRIMARY KEY (`userId`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=latin1;

1 个答案:

答案 0 :(得分:2)

两个都为我工作Boss。

create table users
(   staff int not null,
    remove int not null
);

insert users (staff,remove) values (1,0);
insert users (staff,remove) values (1,1);
insert users (staff,remove) values (2,0);
insert users (staff,remove) values (2,1);


SELECT *
            FROM Users
            WHERE
            Users.staff = 1
            AND Users.remove != 1

SELECT *
            FROM Users
            WHERE
            Users.staff = 1
            AND Users.remove <> 1

由于可为空而进行编辑:

SELECT * FROM Users
WHERE staff=1
and remove <> 1 or remove is null

SELECT * FROM Users
WHERE staff=1
and remove is not null