简体   繁体   中英

mysql selecting data from a table

I have an Users Table with a staff column and a remove column.

I don't want to show Users that have the remove column with the value of 1

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

I don't get an error but my code doesn't work.

this is the schema

    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;

both work for me 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

Edit due to nullability:

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

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

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