S_Shrum has asked for the wisdom of the Perl Monks concerning the following question:

More of an SQL question but it involves DBI. Specifically my question has to do with the * field identifier and the use of.

I'm attempting to construct a query to pass to DBI/AnyData/SQL:Statement to run on a database that contains all the content of my website. The db has roughly 15 or so fields in which all the document data is stored (author, creation date, abstract, etc). I'm trying to query all the fields in the database so that if, say, someone wanted to see all the documents that had the word 'sean' in *one or more* of the fields, they could search for it. I've tried:

  ...WHERE * CLIKE '%sean%'...

But that doesn't work; no results (bad syntax or incorrect logic?). The only way I've found so far that will allow me to get the results I want is to specifically go thru and list each field with a OR separator like:

   ...WHERE Title CLIKE '%sean%' OR Author CLIKE '%sean%' OR Created CLIKE...

Doesn't SQL:Statement support the * field identifier or am I setting this up wrong??? I rather not have to pass any field names if possible. Granted, I could query the data source for the column headers but this just seems like the long way of getting it to work. Also it makes the final SQL string huge which looks ugly when displayed to the user (if I opt to).

TIA

======================
Sean Shrum
http://www.shrum.net

Replies are listed 'Best First'.
Re: DBI - Multi-field SQL: *OR* condition w/o specifying field names...possible?
by Arguile (Hermit) on Oct 05, 2002 at 04:39 UTC

    It's possible. I've never deeply explored SQL::Statement or AnyData, but a generic approach using straight DBI is easy.

    Grab the field names of the table (using either DBI's not-completely-supported-by-all-DBMSs metadata functions or a DBMS specific query). Using that list dynamically construct the ‘OR’ section of the WHERE clause.

    While that method would do what you're asking for, it's not all that useful a query and quickly becomes very very expensive. The prefered approach for such a query is to use a full text search engine, which is generally DBMS dependent.

    A good FTS will allow you to give weightings to fields (such as finding ‘foo’ in the title meaning more than just finding it in the body), search on multiple terms with the option of giving higher weightings when terms are close together, stemming, etc. All with a reasonable cost (I'm talking machine resources).

    Many popular RDMBS packages have FTS engines either included or as additions. Oracle has many included tools as well many third party options, MS SQL Server has some FTS included IIRC, MySQL has rather limited FTS capability, and PostgreSQL has a few options (check under contrib/ or OpenFTS which is actually written largely in Perl). DBMSs’ that aren't listed here probably have such options, I just can't remeber or don't know them well enough.

    Sorry for the brevity of the reply, but it's late and I don't have time to go into the topic deeply. Hope it helps anyways.

Re: DBI - Multi-field SQL: *OR* condition w/o specifying field names...possible?
by screamingeagle (Curate) on Oct 05, 2002 at 01:55 UTC
    AFAIK, neither SQL Server nor Oracle supports the syntax (...* LIKE '%sean%' ) you're trying to use. (I'll go as far as saying no well-known DBMS supports this syntax)

      Do you know if there is another way to do this sort of search in SQL *without* specifying column names?

      After reading the posts that I got I think I may have to look for a full text search tool or do something like writting my own where I load the file into a array and filtering out the records that don't contain the text and passing the resulting array to my content engine which uses DBI as the page construction engine.

      Oh well...thanks for the insight.

      ======================
      Sean Shrum
      http://www.shrum.net