开发者

How to check only the first two letters from a mysql cell?

I want to check a row in an mysql table for the username in php, if the username starts with ab to write an text.

the username is in a cell开发者_StackOverflow and looks like this: ab_12345453.

Thanks, Sebastian

EDIT:

thank you all for your reply. but i need something else. i need this for joomla, i don't need to make an query, i just use: $user->username and i get the logged in username.

so i have the logged in username in a variable. how can i check that the username starts with ab_ ?

thank you again and sorry for the confusion :(


SELECT * FROM bar B where B.username LIKE 'ab%';

See the manual here.


I vote for Artefacto's solution, and add to it a recommendation:

alter table bar add key(username(2));

In case you don't have an index for the column.


use can use also substring

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring


Use LIKE command with single % wildcard to say anychar

SELECT * FROM table WHERE col LIKE 'ab%';

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜