in reply to Re^4: OT - SQL choosing a schema for index tables
in thread OT - SQL choosing a schema for index tables

I should point out that you could make it work but it would be rather complicated !.
Alternatively you could retain the multiple values and use REGEXP in the SELECT . For example

#!perl use strict; use warnings; use DBI; use Data::Dump 'pp'; my $dbh = get_dbh(); $dbh->do('DROP TABLE IF EXISTS trees'); $dbh->do(" CREATE TABLE trees ( id int(11) NOT NULL, uses TEXT , flowering TEXT, ripe TEXT , PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); while (<DATA>){ chomp; $dbh->do('INSERT INTO trees VALUES(?,?,?,?)',undef,split ';'); } my $sql = 'SELECT * FROM trees WHERE (uses REGEXP(?)) AND (flowering REGEXP(?)) AND (ripe REGEXP(?))'; my $ar = $dbh->selectall_arrayref($sql,undef,'cider|juice|jelly','spri +ng','autumn'); for (@$ar){ print join ",",@$_,"\n"; } sub get_dbh{ my $database = "test"; my $user = "root"; my $pw = ""; my $dsn = "dbi:mysql:$database:localhost:3306"; my $dbh = DBI->connect($dsn, $user, $pw, { RaiseError=>1, AutoCommit +=>1 } ); return $dbh; } __DATA__ 1;cider perry;spring winter;spring autumn winter 2;perry juice perry;winter autumn; autumn winter 3;jelly;spring autumn;spring autumn 4;cider cooking;spring autumn winter;spring winter

Or you could select the records using the single value fields where possible and then filter them with a perl routine using regular expressions.

poj

Replies are listed 'Best First'.
Re^6: OT - SQL choosing a schema for index tables
by bangor (Monk) on Aug 28, 2015 at 14:09 UTC
    This is something like how the current system (in PHP) works, but it uses LIKE in the SELECT and sometimes returns incorrect results. I didn't know you could use REGEXP and I can see how it would fix that problem - thank you.

    Your second suggestion could also work for me, but as you imply, it is not guaranteed that the query will contain a value for the single-value fields.