MySQL those things (6) where conditional string quotes

Source: Internet
Author: User
Tags mysql in

Preface: The so-called pit, two meaning, one is the software itself bug, one is the user often make mistakes.

Phper often deals with MySQL in daily development. Especially in small and medium-sized applications without layering, phper development should focus on the implementation of SQL statements.

To the point, the table statement is built:

DROP TABLE IF EXISTS ' test_sql ';

CREATE TABLE ' Test_sql ' (

' id ' int (ten) unsigned not NULL auto_increment COMMENT ' self-increment id ',

' Name ' varchar (+) not NULL DEFAULT ' 0 ' COMMENT ' name ',

PRIMARY KEY (' id ')

) Engine=innodb auto_increment=1 DEFAULT charset=utf8 comment= ' Test form ';

Data on:

INSERT into ' test_sql '

VALUES

(' 1 ', ' 0 '),

(' 2 ', ' www.haodaquan.com '),

(' 3 ', ' 0 '),

(' 4 ', ' 123 '),

(' 5 ', ' 123php '),

(' 6 ', ' 123php on the Road '),

(' 7 ', ' PHP on the Road ');

Okay, here's the Watch:

Here's the question: Look for data with a name value of 0.

So the wrong SQL came out:

Select ' id ', ' name ' from Test_sql where name=0

The results are inconsistent with expectations, such as:

Why?

SQL is also a weak type, but it also returns true when comparing 0 and string.

The correct sql:

Select ' id ', ' name ' from Test_sql where name= ' 0 '

Conclusion: The field of a character type must be quoted when you make a where condition.

MySQL those things (6) where conditional string quotes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.