SELECT语句不正常

时间:2012-12-30 00:24:34

标签: php sql database select

我正在使用此代码从我创建的表中检索数据。

SELECT id,shirt_name,boys FROM shirts WHERE boys IS NOT NULL

不是仅选择boys列输入的行,而是选择所有行。这是我创建表格的方式

CREATE TABLE shirts (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
    shirt_name VARCHAR(20) NOT NULL,
    men VARCHAR(10) NULL,
    women VARCHAR(10) NULL,
    boys VARCHAR(10) NULL,
    girls VARCHAR(10) NULL,
    babies VARCHAR(10) NULL,
)ENGINE=INNODB;

INSERT INTO shirts(shirt_name,men,women,boys,girls,babies) VALUES
    ('Crewneck Tee','me_crn','wo_crn','bo_crn','gi_crn','ba_crn'),
    ('V-Neck Tee','me_vnc','wo_vnc','','',''),
    ('Scoop Neck Tee','','wo_sco','','',''),
    ('Raglan Tee','me_rag','wo_rag','bo_rag','gi_rag',''),
    ('Ringer Tee','me_rin','wo_rin','bo_rin','gi_rin',''),
    ('Cap Sleeve Tee','','wo_cap','','gi_cap',''),
    ('Tank Top','me_tan','wo_tan','bo_tan','gi_tan',''),
    ('Spaghetti Strap','','wo_spa','','',''),
    ('Hoodie','me_hod','wo_hod','bo_hod','gi_hod','ba_hod');

我做错了什么?

2 个答案:

答案 0 :(得分:4)

您未在NULL列中插入boys的任何记录。只记录它是一个空字符串。空字符串('')与NULL不同,因此它与IS NOT NULL匹配。

答案 1 :(得分:1)

SELECT id,shirt_name,boys FROM shirts WHERE boys != ''

您需要使用此数据吗