Multi column search

Questions, tips and tricks and techniques for scripting in Servoy

Multi column search

Postby 4xjbh » Thu Jul 23, 2009 2:49 pm

Do you do a multi column search via find()/search() or via sql query?

I have a single global search field and I want to search the project_number (int) and project_name (string) columns for the value.

I know how to search via sql but cannot work out how to do it using find & search. Is one better than the other?

Thx
Regards,

James
4xjbh
 
Posts: 146
Joined: Fri Feb 24, 2006 8:06 am
Location: Brisbane, Australia

Re: Multi column search

Postby ROCLASI » Thu Jul 23, 2009 3:12 pm

Hi James,

Yes you can seach on multiple columns in one search:

Code: Select all
if ( controller.find() ) {
   column1 = globals.mySearchVar;
   column2 = globals.mySearchVar;
   controller.search();
}


This is the equivalent of the following SQL:
Code: Select all
SELECT id FROM theTable WHERE column1=? AND column2=?


But I think you want to do an OR search. So the value matches in project number OR project name.
Then you code would be like this:

Code: Select all
if ( controller.find() ) {
   column1 = globals.mySearchVar;
   controller.newRecord(); // new search criteria
   column2 = globals.mySearchVar;
   controller.search();
}


Which translates into SQL like so:
Code: Select all
SELECT id FROM theTable WHERE column1=? OR column2=?


Also use %, ! and # where needed.

Hope this helps
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: Multi column search

Postby 4xjbh » Thu Jul 23, 2009 3:25 pm

Thanks, helps alot. Do I need to convert the integer (project_number) to a string to do a wildcard search?
Regards,

James
4xjbh
 
Posts: 146
Joined: Fri Feb 24, 2006 8:06 am
Location: Brisbane, Australia

Re: Multi column search

Postby ROCLASI » Thu Jul 23, 2009 3:38 pm

4xjbh wrote:Thanks, helps alot. Do I need to convert the integer (project_number) to a string to do a wildcard search?


Hmm..I think you better use SQL then to perform the search. Since you need to cast the values in the database (thus the field) to be able to use wildcards. I don't think you can otherwise use wildcards in integer data.

Pseudo SQL:
Code: Select all
SELECT id FROM myTable WHERE cast(project_id, 'string') LIKE '%23456%' OR projectname LIKE '%23456%'

Depending on the database brand you need to change the cast function (I assume you use MSSQL or Sybase since you mentioned 'convert').

This SQL you can then use to load the foundset like so:
Code: Select all
var _sQuery = "SELECT id FROM myTable WHERE cast(project_id, 'string') LIKE ? OR projectname LIKE ?"; // pseudo sql
var _sArg = "%" + globals.mySearchVar + "%";
controller.loadRecords(_sQuery, [_sArg, _sArg]);


Hope this helps.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: Multi column search

Postby 4xjbh » Fri Jul 24, 2009 3:03 pm

Thanks,

just using the following worked fine.

Code: Select all
var _sQuery = "SELECT project_id FROM project WHERE project_number LIKE ? OR project_name LIKE ?"; // pseudo sql
Regards,

James
4xjbh
 
Posts: 146
Joined: Fri Feb 24, 2006 8:06 am
Location: Brisbane, Australia

Re: Multi column search

Postby ROCLASI » Fri Jul 24, 2009 3:22 pm

Interesting. Do you also use % in the searchstring on the int column ?
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: Multi column search

Postby 4xjbh » Fri Jul 24, 2009 3:30 pm

Yep. '%' + globals.srch_main + '%'

Works a treat. ....at the moment.
Regards,

James
4xjbh
 
Posts: 146
Joined: Fri Feb 24, 2006 8:06 am
Location: Brisbane, Australia


Return to Methods

Who is online

Users browsing this forum: No registered users and 39 guests

cron