文档主页
用于 VS Code 的 MySQL Shell


用于 VS Code 的 MySQL Shell  /  TypeScript  /  数据库笔记本:使用 SELECT 语句检索数据

正式发布前:2024-07-17

9.2 数据库笔记本:使用 SELECT 语句检索数据

数据库笔记本:返回一行的基本查询

runSql(
"SELECT Name as label, District as district, Population as value FROM world.city WHERE Name = 'Kabul' ",
  (res: ResultSetRows) => {
    print(res);
  }
);

输出为

[
    {
          "label": "Kabul",
          "district": "Kabol",
          "value": 1780000
    }
]

数据库笔记本:返回多行的基本查询

const graphData = [];

runSqlIterative(
  "SELECT rating as label , count(rating) as value FROM sakila.film " +
    "GROUP BY rating",
  (res: IResultSetData) => {
    if (res.rows) {
      res.rows.forEach((row, index) => {
        graphData.push({
          label: row[1] as string,
          value: 25,
        });
      });
    }
    if (res.requestState.type === "OK") {
      print(res);
    }
  }
);

输出为

{
  "requestState": {
    "type": "OK",
    "msg": "Full result set consisting of 5 rows transferred."
  },
  "requestId": "6ee28aa2-2727-42d9-2bd9-d60f8d4abb64",
  "rows": [
  [
      "PG",
      194
    ],
    [
      "G",
      178
    ],
    [
      "NC-17",
      210
    ],
    [
      "PG-13",
      223
    ],
    [
      "R",
      195
    ]
  ],
  "columns": [
  {		
      "name": "label",
      "type": "ENUM",
      "length": 20
  },
  {
      "name": "value",
      "type": "INTEGER",
      "length": 21
  }
  ],
  "done": true,
  "totalRowCount": 5,
  "executionTime": 0.0009739398956298828
}