我有一张桌子,我正在阅读它的n%记录.为此,我准备了一个查询:
SET @rows := (SELECT COUNT(*)*0.5 FROM trending);
PREPARE STMT FROM 'SELECT * FROM trending ORDER BY Count LIMIT ?';
EXECUTE STMT USING @rows;
我得到的输出是:
SET @rows := (SELECT COUNT(*)*0.5 FROM trending);# MysqL returned an empty result set (i.e. zero rows). PREPARE STMT FROM 'SELECT * FROM trending ORDER BY Count LIMIT ?';# MysqL returned an empty result set (i.e. zero rows). EXECUTE STMT USING @rows;# Rows: 8
我怎样才能从表中获取记录?
解决方法:
SET @rows := (SELECT COUNT(*)*0.5 FROM trending);
PREPARE STMT FROM 'SELECT * FROM trending order by somecolumn LIMIT ?';
EXECUTE STMT USING @rows;
在我的系统上工作正常.奇数行可以得到一个综合
编辑:
create table thing2
( id int auto_increment primary key,
theWhat varchar(40) not null,
`count` int not null
);
插入5行:
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 |
+----+-----------+-------+
此修订是由于提到PHP.因此,这显示了一个多查询,使用从该查询的特定行给出的结果集.显示使用明确保留其值的@变量.并且为多查询本身设置了一个heredoc变量$thesql.
<?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;
}
?>
浏览器输出:
I am connected and feel happy.
1 anchovies 6
2 tomato 1
3 cat 99
上面做的/有时间的长短是因为我们在该循环中基于传递的查询进行了4次.
请参见手册页面mysqli.multi-query
和mysqli.store-result
.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。