mysql select 存在_如何使用MySQL SELECT 1检查值是否存在?

为此,请使用SELECT 1,如以下语法所示:select 1 from yourTableName where yourColumnName=yourValue;

如果上面的返回1,则表示MySQL数据库中存在值。我们首先来看一个示例并创建一个表-mysql> create table DemoTable

(

StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,

StudentName varchar(40),

StudentAge int

);

使用插入命令在表中插入一些记录-mysql> insert into DemoTable(StudentName,StudentAge) values('Chris',21);

mysql> insert into DemoTable(StudentName,StudentAge) values('David',20);

mysql> insert into DemoTable(StudentName,StudentAge) values('Bob',22);

mysql> insert into DemoTable(StudentName,StudentAge) values('Tom',19);

使用select语句显示表中的所有记录-mysql> select *from DemoTable;

这将产生以下输出-+-----------+-------------+------------+

| StudentId | StudentName | StudentAge |

+-----------+-------------+------------+

|        1 | Chris        |         21 |

|        2 | David        |         20 |

|        3 | Bob          |         22 |

|        4 | Tom          |         19 |

+-----------+-------------+------------+

4 rows in set (0.00 sec)

现在让我们检查MySQL数据库中是否存在值-mysql> select 1 from DemoTable where StudentName='Bob';

这将产生以下输出-+---+

| 1 |

+---+

| 1 |

+---+

1 row in set (0.00 sec)

你可能感兴趣的:(mysql,select,存在)