简体   繁体   中英

mysql query to select user count with Like clause

I have a forum, there's an option for user to change their names. Also i have staff members with names like [ADM]James . I want to prevent users to change his name in James.

Example: I have a memeber from staff with name [ADM]James , users should not be able to change his name in simply James.

There's my query: SELECT COUNT(*) FROM users WHERE name='James' LIKE '[%%' However this query return strange numbers it always return whole count of users.

What i do wrong?

EDIT, WHAT IS UNCLEAR?

I have 2 staff members. Admins and moderators, admins have tag name [ADM] moderators [MOD]. Example two names: [ADM]James [MOD]Jeff.. When a user try to change his name in James count should return 1. Because there is already one staff memeber with name [ADM]James.

CREATE TABLE `user`  (
  `id` int(0) NOT NULL AUTO_INCREMENT,
  `user` varchar(24) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'NONAME',
) ENGINE = MyISAM AUTO_INCREMENT = 191782 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;

Use below query,

SELECT COUNT(*) FROM users WHERE name LIKE '%]%'

SELECT user, COUNT(*) FROM users WHERE name LIKE '%[ADM]%' group by user;
SELECT COUNT(*) FROM users WHERE name LIKE '%James%'

Will count every name that has 'James' on it.

WHERE name='James' LIKE '[%%' is not a valid where clause

The syntax of the LIKE operator is expr LIKE pat [ESCAPE 'escape_char'] You can either use % or _

  • % matches any number of characters, even zero characters.

  • _ matches exactly one character.

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