Pergunta

I'm into networking so the data you see is switch/port information.

The problem:

I only want the "ge-0/0/x" part of the SQL query and I want it sorted by "ge-0/0/x" and I only want to "select distinct" of the "ge-0/0/x" part(only 1 port, if you follow me).

As there are some dots on some results I've figured out that i could use rtrim to get rid of them. There are some dashes(-) in the result as well so any help to get rid of them as well is much appreciated.

PHP:

// "nodes" is the number of swithes, thus the ge-[node].
$host_name = 'switch_host_name';
$nodes = 2;
for($node=0;$node<$nodes;$node++){
    $ge = '%: ge-'.$node.'%';
    $portquery = mysql_query("SELECT service_description FROM service WHERE host_name='$host_name' AND service_description LIKE '$ge'") or die(mysql_error());
    while($portarray = mysql_fetch_array($portquery)){
        $port = substr($portarray['service_description'],8,10);
        echo '<div id="'.$port.'_'.$host_name.'">'.$port.'</div>';
    }
    echo '<br />';
}

SQL example:

+-------------------+----------------------------------+
| host_name         | service_description              |
+-------------------+----------------------------------+
| switch_host_name  | IF 151: ge-0/0/28.0 - Status     |
| switch_host_name  | IF 153: ge-0/0/29 - Status       |
| switch_host_name  | IF 155: ge-0/0/3 - Description   |
| switch_host_name  | IF 155: ge-0/0/3 - Status        |
| switch_host_name  | IF 155: ge-0/0/3.0 - Traffic     |
| switch_host_name  | IF 195: ge-0/0/5 - Description   |
| switch_host_name  | IF 195: ge-0/0/5 - Status        |
| switch_host_name  | IF 195: ge-0/0/5.0 - Traffic     |
+-------------------+----------------------------------+

Any clues, links, guides, tuts that you can point me to is much appreciated. Of course a solution would be great!

Thanks in advance.

Foi útil?

Solução

If you have MANY of these records, I'd suggest you massage the strings to extract the relevant portions into dedicated DB fields at the time you're inserting the records into the DB. While I understand this is device config data, storing formatted data in a field negates the purpose of having a relational database, as you're finding out. Doing sorting/filtering becomes very difficult.

That being said, you can probably do some basic string manipulations in a query to extract your ge data:

SELECT
    @start := LOCATE('ge-', service_description),
    @end := LOCATE(' - ', service_description),
    SUBSTR(service_description, @start, @end - @start) AS ge
FROM ...
GROUP BY ge
ORDER BY ge

This presumes that there'll only be one ge- and one - string per record in that field.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top