All Products
Search
Document Center

Tablestore:Query data by executing SQL statements

Last Updated:Sep 29, 2026

This topic describes how to use Tablestore SDK for PHP to query data by executing SQL statements.

Usage notes

SQL query is supported in Tablestore SDK for PHP V5.1.0 and later. Make sure that a supported version is installed.

For the version history of Tablestore SDK for PHP, see Version history of Tablestore SDK for PHP.

Prerequisites

Parameters

SQLQueryRequest contains the following parameter.

Name

Type

Description

query (required)

String

The CREATE TABLE statement that defines the mapping table name, fields, primary key, and query engine. Data table mappings and search index mappings use different syntax.

Example

The following example shows how to use the SELECT `PK0`, `boolean`, `long`, `geo` FROM `tableName` LIMIT 10; statement to query data in the tableName table and return a maximum of 10 rows. The system returns the request type of the query statement, the schema of the return value, and the returned results.

$request = array(
    'query' => 'SELECT `PK0`, `boolean`, `long`, `geo` FROM `tableName` LIMIT 10;',
);
$response = $this->otsClient->sqlQuery($request);
$sqlRows = $response['sql_rows'];

// print all data metrix
$lines = '';
for ($i = 0; $i < $sqlRows->rowCount; $i++) {
    $line = '';
    for ($j = 0; $j < $sqlRows->columnCount; $j++) {
        $line = $line . (is_null($sqlRows->get($j, $i)) ? "null" : $sqlRows->get($j, $i)) . "\t";
    }
    $lines = $lines . $line . "\n";
}
print $lines;
$sqlRows = $response['sql_rows'];

FAQ

References