Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to force case sensitivity in MySQL / Rails for a single find?

I'm doing some searching of tags, and some users like "cat" while others like "Cat" Go figure...

Anyways, is there a way to force a particular find to be case sensitive? Such as:

Tag.find(:some-special-option-here)

Any ideas?

like image 541
Micah Avatar asked Nov 05 '08 05:11

Micah


2 Answers

You can also do a case-sensitive search without changing your column properties.

SELECT * FROM mytable WHERE myfield='Value' 

This query matches:

  • Value
  • value
  • VALUE
  • vAlUe
  • and so on

While...

SELECT * FROM mytable WHERE BINARY myfield='Value'

Matches only:

  • Value
like image 148
Stefan Gehrig Avatar answered Oct 12 '22 01:10

Stefan Gehrig


You can make all strings case sensitive when you create the table by adding "COLLATE utf8_bin" to the :options string when creating the table. For example:

create_table( "page_infos", :force => true, :options => "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_bin") do |t|
      t.string   "title",       :limit => 120
end
like image 23
Paul Grayson Avatar answered Oct 12 '22 01:10

Paul Grayson