Make 3 simultaneous searches on the same table

1

I would like to know if it is possible to perform 3 simultaneous searches on the same table. The situation is as follows: I have a table that contains the columns:

Car, Material, IP, date

Then I have to search:

1 - Quantos carros foram cadastrados na ultima hora
2 - Quantos Materiais foram cadastrados na última 2 horas
3 - Quantos Ips acessaram nos últimos 30 minutos

It is possible for me to perform these 3 simultaneous searches, where I have to return the number of data found in each of them.

The way I do it is as follows:

$conn->prepare("QUERY 1");
$conn->prepare("QUERY 2");
$conn->prepare("QUERY 3");

I call the prepare 3 times, I would like to minimize this, call only once and return the number of data separated by the occurrence number:

Query 1= 10 
Query 2= 15
Query 3= 17

Is this possible or will I have to look the way I currently do, all separately?

    
asked by anonymous 20.03.2015 / 22:51

2 answers

4

Subquery no from seems to be an alternative:

SELECT a.carros, b.materiais, c.ips FROM 
    (SELECT carros ...) AS a, 
    (SELECT materiais ...) AS b, 
    (SELECT ips ...) AS c

I hope to have helped, hug

    
20.03.2015 / 23:20
4

An output with mysqli is to use multi query .

Here is an example of PHP documentation :

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query  = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

/* execute multi query */
if ($mysqli->multi_query($query)) {
    do {
        /* store first result set */
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
                printf("%s\n", $row[0]);
            }
            $result->free();
        }
        /* print divider */
        if ($mysqli->more_results()) {
            printf("-----------------\n");
        }
    } while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();
?>

Notes:

  • mysqli_store_result() Download a result set from the active query
  • mysqli_next_result() Prepares the next result of multi_query
  • mysqli_more_results() Checks for any further results of a multi query to be processed
20.03.2015 / 23:39