php sql suche

Ok, noch ein anderer Vorschlag:

PHP:
<?php
$suche = "golf 8967 vw 12345 klima";
$suche = str_replace(" ", "|", $suche);

$sql = "
	SELECT * FROM article
	WHERE ARTIKELNR REGEXP '" .$suche ."'
	OR LFDNR REGEXP '" .$suche ."'
	OR TEXT  REGEXP '" .$suche ."'
	OR BEZEICHNUNG  REGEXP '" .$suche ."'
	OR SUCHARTIKELNR REGEXP '" .$suche ."' ";

echo $sql;
?>

Ergibt die folgende Anweisung:

SQL:
SELECT * FROM article WHERE
    ARTIKELNR REGEXP 'golf|8967|vw|12345|klima'
OR
    LFDNR REGEXP 'golf|8967|vw|12345|klima'
OR
    TEXT REGEXP 'golf|8967|vw|12345|klima'
OR
    BEZEICHNUNG REGEXP 'golf|8967|vw|12345|klima'
OR
    SUCHARTIKELNR REGEXP 'golf|8967|vw|12345|klima'

Ich sehe gerade erst das die einzelnen Bedingungen mit AND verknüpft sein sollen.
Geht mit obiger Abfrage auch (OR durch AND ersetzen), nur muss dann auch wirklich einer der Begriffe bei jeder Bedingung gefunden werden.
 
Zuletzt bearbeitet:
Das ist: „In jeder Spalte muss mindestens einer der Suchbegriffe auftauchen.“
? Jede Spalte muss getroffen werden.

Die folgende Funktion ist: „Jeder Suchbegriff muss in mindestens einer Spalte auftauchen.“
? Jeder Begriff muss getroffen werden.

(Sorry, der Code ist etwas umständlich.)

PHP:
<?php

function f(array $columns, array $keywords)
{
    $indent  = str_repeat(' ', 4);
    $indent2 = str_repeat(' ', 8);

    $wherePart = '';

    $separator = '';

    foreach ($keywords as $keyword) {
        $separator2 = '';
        $tmp = '';

        foreach ($columns as $column) {
            $tmp .= $indent2 . $separator2 . '`' . $column . '` LIKE "%' . $keyword . '%"' . "\n";
            $separator2 = 'OR ';
        }

        $wherePart .= $indent . $separator . "(\n" . $tmp . $indent . ")\n";

        $separator = "AND\n" . $indent;
    }

    return $wherePart;
}

$columns = array('col1', 'col2', 'col3', 'col4', 'col5', 'col6');

$keywords = array('golf', 'sierra', 'foxtrott', 'kirk', 'spock', 'uhura');

echo f($columns, $keywords);

Code:
    (
        `col1` LIKE "%golf%"
        OR `col2` LIKE "%golf%"
        OR `col3` LIKE "%golf%"
        OR `col4` LIKE "%golf%"
        OR `col5` LIKE "%golf%"
        OR `col6` LIKE "%golf%"
    )
    AND
    (
        `col1` LIKE "%sierra%"
        OR `col2` LIKE "%sierra%"
        OR `col3` LIKE "%sierra%"
        OR `col4` LIKE "%sierra%"
        OR `col5` LIKE "%sierra%"
        OR `col6` LIKE "%sierra%"
    )
    AND
    (
        `col1` LIKE "%foxtrott%"
        OR `col2` LIKE "%foxtrott%"
        OR `col3` LIKE "%foxtrott%"
        OR `col4` LIKE "%foxtrott%"
        OR `col5` LIKE "%foxtrott%"
        OR `col6` LIKE "%foxtrott%"
    )
    AND
    (
        `col1` LIKE "%kirk%"
        OR `col2` LIKE "%kirk%"
        OR `col3` LIKE "%kirk%"
        OR `col4` LIKE "%kirk%"
        OR `col5` LIKE "%kirk%"
        OR `col6` LIKE "%kirk%"
    )
    AND
    (
        `col1` LIKE "%spock%"
        OR `col2` LIKE "%spock%"
        OR `col3` LIKE "%spock%"
        OR `col4` LIKE "%spock%"
        OR `col5` LIKE "%spock%"
        OR `col6` LIKE "%spock%"
    )
    AND
    (
        `col1` LIKE "%uhura%"
        OR `col2` LIKE "%uhura%"
        OR `col3` LIKE "%uhura%"
        OR `col4` LIKE "%uhura%"
        OR `col5` LIKE "%uhura%"
        OR `col6` LIKE "%uhura%"
    )

JesusFreak777 hat gesagt.:
jup, wobei da immernoch die berücksitigung fehlt, das ja s1 & s2 in einer zelle sein könnte!

... oder doch nicht?

Nope. :)
 
Zuletzt bearbeitet:

Neue Beiträge

Zurück