Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

SQL pulling information

by andrew (Acolyte)
on Oct 16, 2002 at 21:31 UTC ( [id://205850]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: SQL pulling information
by insensate (Hermit) on Oct 16, 2002 at 23:24 UTC
    Why not just
    select m_category from table where INSTR(m_category,'|44|') > 0 or SUBSTRING(m_category,0,3) = '44|' or SUBSTRING_INDEX(m_category,'|',-1) = '44';

    -Jason update: changed to accomodate 44 as the first or last column
Re: SQL pulling information
by rdfield (Priest) on Oct 17, 2002 at 09:29 UTC
    Looks to me like you have a significant design problem. The elements divided by the | should normally be split out into another table unless you have compelling reasons for leaving them together like that.

    rdfield

Re: SQL pulling information
by robartes (Priest) on Oct 16, 2002 at 21:45 UTC
    I often find that asking a question is a prerequisite to getting an answer.

    What exactly is yours?

    CU
    Robartes-

      How would I use perl to search through the database and search through the bars just to find a number. And if they find a line that has it then pull information.
        Use DBI to connect to the database, and launch your SQL statement, then use split to pull apart the result so you get the individual numbers, which you can then manipulate to your heart's content:
        use strict; use DBI; my $mynumber=44; # Boilerplate connect code skipped. my $sth=$dbh->prepare("select column from table where column LIKE '%$m +ynumber%'"); $sth->execute; while (my $ref=$sth->fetchrow_arrayref) { my @numbers=split /|/, $ref->[0]; # do something with @numbers, optionally if /44/ } print "This code is untested. Beware of dragons.\n";
        Does this help you?

        CU
        Robartes-

Re: SQL pulling information
by andrew (Acolyte) on Oct 16, 2002 at 21:59 UTC
    Would I be able to use like a rex in my SQL statement like
    $sth = $dbh->prepare("SELECT * FROM items WHERE m_category =~ /\|?44\| +?/");
    Would that ever be possible or do that have a function I heard of REGEXP but dont understand.
      Try searching around mysql.com's documentation a little bit. I knew how to do this but I wanted to find the documentation. You know what they say, give a man a fish, he eats for a day, teach a man to fish, he can eat for life. In that spirit I scrounged up a link for you.

      All the examples look like:
      SELECT "string" REGEXP "some regex";
      all this does is return 1 or 0. in your situation, your select will look something like this:
      SELECT col1, col2, ... FROM table WHERE col1 REGEXP 'your regex'


      I hope this helps.

      -brad..
Re: SQL pulling information
by andrew (Acolyte) on Oct 16, 2002 at 22:28 UTC
    Alright got it working with regexp works great, thnks for your help :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://205850]
Approved by chip
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-25 06:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found