sql >> Database teknologi >  >> RDS >> Mysql

Hvordan får jeg resultatet af en sætning efter EXECUTE-sætning i mySQL?

SET @rows := (SELECT COUNT(*)*0.5 FROM trending);
PREPARE STMT FROM 'SELECT * FROM trending order by somecolumn LIMIT ?';
EXECUTE  STMT USING @rows;

fungerer fint på mit system. ulige antal rækker giver dig en round-up

Rediger:

create table thing2
(   id int auto_increment primary key,
    theWhat varchar(40) not null,
    `count` int not null
);

Indsæt 5 rækker:

insert thing2(theWhat,`count`) values ('anchovies',6),('tomato',1),('cat',99),('mouse',8),('spoon',70);

SET @rows := (SELECT COUNT(*)*0.5 FROM thing2);
PREPARE STMT FROM 'SELECT * FROM thing2 LIMIT ?';
EXECUTE  STMT USING @rows;


count might need to be in backticks (not on my system), if that is actually a column and you are doing
 the following :

SET @rows := (SELECT COUNT(*)*0.5 FROM thing2);
PREPARE STMT FROM 'SELECT * FROM thing2 order by `count` LIMIT ?';
EXECUTE  STMT USING @rows;
+----+-----------+-------+
| id | theWhat   | count |
+----+-----------+-------+
|  2 | tomato    |     1 |
|  1 | anchovies |     6 |
|  4 | mouse     |     8 |
+----+-----------+-------+

SET @rows := (SELECT COUNT(*)*0.5 FROM thing2);
PREPARE STMT FROM 'SELECT * FROM thing2 order by theWhat LIMIT ?';
EXECUTE  STMT USING @rows;
+----+-----------+-------+
| id | theWhat   | count |
+----+-----------+-------+
|  1 | anchovies |     6 |
|  3 | cat       |    99 |
|  4 | mouse     |     8 |
+----+-----------+-------+

Revision 1 (PHP vist)

Denne revision skyldes indstillingen, der nævner PHP. Så dette viser en multi-forespørgsel, ved hjælp af resultatsættet givet fra en bestemt linje i den forespørgsel. Viser brugen af ​​en @ variabel, der klart bevarer sin værdi. Og en heredoc variabel $theSql indstilles til selve multi-forespørgslen.

<?php
    error_reporting(E_ALL);
    //mysqli_report(MYSQLI_REPORT_ALL);
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    ini_set('display_errors', 1);

    try {
        $mysqli= new mysqli('localhost', 'dbusername', 'thePassword', 'theDbname'); // tweak accordingly
        if ($mysqli->connect_error) {
            die('Connect Error (' . $mysqli->connect_errno . ') '
                . $mysqli->connect_error);
        }
        echo "I am connected and feel happy.<br/>";

        // Note the 3rd statment below (the EXECUTE) and the code below it that cares about output for the 3rd one only

$theSql = <<<SQL
    SET @rows := (SELECT COUNT(*)*0.5 FROM thing2); 
    PREPARE stmt123 FROM 'SELECT * FROM thing2 LIMIT ?'; 
    EXECUTE stmt123 USING @rows; 
    DEALLOCATE PREPARE stmt123;
SQL;
        $shouldDebug=false; // change to true to see more debug info (such as the skipped multi-query results)
        $theCounter=0;
        $weCareAbout=3; // the 3rd line of the command: "EXECUTE stmt123 USING @rows; "
        if ($mysqli->multi_query($theSql)) {
            do { // Note this loop poached from http://php.net/manual/en/mysqli.multi-query.php
                if ($shouldDebug) echo "1a.<br/>";
                $theCounter++;
                // store first result set
                if ($result = $mysqli->store_result()) {
                    if ($shouldDebug)  echo "1b.<br/>";
                    if ($theCounter==$weCareAbout) {
                        while ($row = $result->fetch_row()) {
                            echo $row[0]." ".$row[1]." ".$row[2]."<br>";
                        }
                    }
                    if ($shouldDebug)  echo "1c.<br/>";
                    $result->free();
                    if ($shouldDebug)  echo "1d.<br/>";
                }
                // print divider 
                if ($mysqli->more_results() && $shouldDebug) {
                    printf("-----------------\n"); 
                }
                if ($shouldDebug) "1e.<br/>";
            } while ($mysqli->next_result() && $mysqli->more_results());
            // above line to avoid error: Strict Standards: mysqli::next_result(): There is no next result set. ...
            // ...Please, call mysqli_more_results()/mysqli::more_results() to check whether to call ...
            //
            // the Manual page is not exactly clear on this. In fact, faulty.
            // http://php.net/manual/en/mysqli.multi-query.php
        }

        $mysqli->close();   // just showing it, what the heck
    } catch (mysqli_sql_exception $e) { 
        throw $e; 
    } 
?>

Browser output:

I am connected and feel happy.
1 anchovies 6
2 tomato 1
3 cat 99

Det lange og korte af ovenstående do/while er, at vi er i den løkke 4 gange baseret på den beståede forespørgsel.

Se venligst manualsiden mysqli.multi-query og mysqli.store-result .




  1. Sådan fungerer CONVERT() i MariaDB

  2. Socketfil /var/pgsql_socket/.s.PGSQL.5432 mangler i Mountain Lion (OS X Server)

  3. Kan ikke oprette PoolableConnectionFactory (Adgang nægtet for bruger ''@'localhost'

  4. viser én kolonne flere gange med data fra 3 forskellige datointervaller