简体   繁体   English

检查 mysql 表的列中是否存在值

[英]Check if a value exists in a column in mysql table

I have a table called accountinfo with a row called username.我有一个名为 accountinfo 的表,其中有一行名为 username。

When i run the register.php i want to check if the username already exists in the mysql table or not.当我运行 register.php 时,我想检查 mysql 表中是否已经存在用户名。

Anyone have any ideas?有人有想法么?

SELECT count(*) AS num_user 
FROM   accountinfo 
WHERE  username = "your_user_name"

which will give you a non-zero value if your_user_name already exists in the database.如果您的用户your_user_name已经存在于数据库中,这将为您提供一个非零值。 If it doesn't exist, then you will get zero.如果它不存在,那么你将得到零。

PS附言

If you don't want duplicate username in the database, then you better add uniqueness constraints in your table.如果您不想在数据库中重复用户名,那么您最好在表中添加唯一性约束。

To add uniqueness constraints, use this query -要添加唯一性约束,请使用此查询 -

ALTER TABLE accountinfo ADD CONSTRAINTS unique_username UNIQUE (username);

Adding this uniqueness constraint will save you from a lot of troubles.添加此唯一性约束将使您免于很多麻烦。

Just query for the username:只需查询用户名:

SELECT `username` FROM `accountinfo` WHERE `username` = :existing_username
-- or
SELECT `username` FROM `accountinfo` WHERE `username` LIKE :existing_username

I suggest you quickly learn SQL queries:)建议你赶紧学SQL查询:)

SELECT * from accountinfo where username = 'something'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM