网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 服务器与数据库 > MySQL
User Name
Password
 
FAQ Members List Calendar Cheap Glasses

Reply
 
Thread Tools Display Modes
  #1   IP: 204.45.15.210
Old 2015-12-19, 11:30 PM
Vintondale Vintondale is offline
初级会员
 
Join Date: 2007-04-25
Posts: 1
Vintondale 现在声名狼藉
Default MySQL查询时强制区分大小写的方法

MySQL查询时强制区分大小写的方法



平时很少会考虑数据存储需要明确字符串类型字段的大小写,MySQL默认的查询也不区分大小写。但作为用户信息,一旦用户名重复,又会浪费很多资源。再者,李逵、李鬼的多起来,侦辨起来很困难。要做到这一点,要么在建表时,明确大小写敏感(字段明确大小写敏感)。如果通盘数据库所有字段都需要大小写敏感,不如在字符集设置时做好调整。不过,通常不建议这么做。


如果跟我一样,数据库已经在线上跑了,一个表上百万条数据,做字段类型变更有可能导致数据库宕机。那么好吧,在查询时,多加个单词好了!



例如,一般查询:

Sql代码

SELECT * FROM U WHERE name LIKE 'a%';

SELECT * FROM U WHERE name LIKE 'A%';

其结果是一样的,为了区分'A%'和'a%',可以这么做:



Sql代码

SELECT * FROM U WHERE binary name LIKE 'a%';

SELECT * FROM U WHERE binary name LIKE 'A%';

仅仅多了一个binary,就可以得到不同的结果!


当然,如果需要建表时强制区分大小写,可以这么写:



Sql代码

create table table_name(

name varchar (20) binary

);

就这么简单!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 06:30 AM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.