当前位置: 动力学知识库 > 问答 > 编程问答 >

mysql - Using NOT LIKE in WHERE Clause

问题描述:

The User Table is like below,

name | username | email

---------------------------------

Joe | joe | [email protected]

---------------------------------

John | ABC\john | [email protected]

---------------------------------

Kate | kate | [email protected]

---------------------------------

Ron | ABC\ron | [email protected]

---------------------------------

The mySQL query to select all fields from the table where the username in the table doesn't start with "ABC\". Since the user logs in to an application using the active directory credentials, the user name is stored in to with Domain\Username (Eg: ABC\username). I tried to create the query but it is not working.

Select * from User where username NOT LIKE 'ABC\\\\%_'

But is not working.It is pulling all the user records.

name | username | email

---------------------------------

Joe | joe | [email protected]

---------------------------------

John | ABC\john | [email protected]

---------------------------------

Kate | kate | [email protected]

---------------------------------

Ron | ABC\ron | [email protected]

---------------------------------

But I need users whose username doesn't starts with "ABC\".

name | username | email

---------------------------------

John | ABC\john | [email protected]

---------------------------------

Ron | ABC\ron | [email protected]

---------------------------------

I tried to filter with respect to the email address as below it is working fine

Select * from User where email NOT LIKE '_%@abc.com'

I am new to queries and can manage to write simple queries but this one I am not sure if I am following the correct way.

网友答案:

Your Query works fine for me. I think your problem is using backslash in a string that would be your query.

            $not_like= "ABC\\%";
            $statement = $db->prepare("SELECT * FROM `User` WHERE `email` NOT LIKE ?");
            $statement->bind_param('s', $not_like);
            $statement->execute();
            $result = $statement->get_result();
            while($get_users = $result->fetch_assoc()) {
                echo($get_users['username']);
            }
            $result2->free();
网友答案:

I am not sure who posted the answer

Select * from User where username NOT LIKE 'ABC\%' 

that works here. I was following link it was not working, not sure why. Thanks everybody

分享给朋友:
您可能感兴趣的文章:
随机阅读: