Hướng dẫn how do i run two sql queries in mysql? - làm cách nào để chạy hai truy vấn sql trong mysql?

Tôi đang sử dụng MySQL Workbench CE cho Windows phiên bản 5.2.40.

Tôi muốn thực hiện các truy vấn SQL sau đây với nhau. Tuy nhiên, tôi chỉ có thể thực thi các truy vấn SQL bằng cách thực hiện truy vấn CREATE TABLE trước tiên, sau đó thực thi truy vấn INSERT INTO và sau đó thực hiện truy vấn SELECT.

CREATE TABLE testTable[
    Name VARCHAR[20],
    Address VARCHAR[50],
    Gender VARCHAR[10]
]

INSERT INTO testTable
    VALUES
    ['Derp', 'ForeverAlone Street', 'Male'],
    ['Derpina', 'Whiterun Breezehome', 'Female']

Select * FROM testTable

Vậy làm cách nào để thực hiện các truy vấn CREATE TABLE, INSERT INTOSELECT bằng một cú nhấp chuột?

3.6.3 & NBSP; Hỗ trợ thực thi nhiều câu lệnh

Theo mặc định, mysql_real_query[]mysql_query[] Giải thích đối số chuỗi câu lệnh của họ là một câu lệnh được thực thi và bạn xử lý kết quả theo liệu câu lệnh tạo ra một tập kết quả [một tập hợp các hàng, như đối với SELECT] hoặc số lượng hàng bị ảnh hưởng [ Đối với

/* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
    db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
  printf["mysql_real_connect[] failed\n"];
  mysql_close[mysql];
  exit[1];
}

/* execute multiple statements */
status = mysql_query[mysql,
                     "DROP TABLE IF EXISTS test_table;\
                      CREATE TABLE test_table[id INT];\
                      INSERT INTO test_table VALUES[10];\
                      UPDATE test_table SET id=20 WHERE id=10;\
                      SELECT * FROM test_table;\
                      DROP TABLE test_table"];
if [status]
{
  printf["Could not execute statement[s]"];
  mysql_close[mysql];
  exit[0];
}

/* process each statement result */
do {
  /* did current statement return data? */
  result = mysql_store_result[mysql];
  if [result]
  {
    /* yes; process rows and free the result set */
    process_result_set[mysql, result];
    mysql_free_result[result];
  }
  else          /* no result set or error */
  {
    if [mysql_field_count[mysql] == 0]
    {
      printf["%lld rows affected\n",
            mysql_affected_rows[mysql]];
    }
    else  /* some error occurred */
    {
      printf["Could not retrieve result set\n"];
      break;
    }
  }
  /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
  if [[status = mysql_next_result[mysql]] > 0]
    printf["Could not execute statement\n"];
} while [status == 0];

mysql_close[mysql];
1,
/* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
    db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
  printf["mysql_real_connect[] failed\n"];
  mysql_close[mysql];
  exit[1];
}

/* execute multiple statements */
status = mysql_query[mysql,
                     "DROP TABLE IF EXISTS test_table;\
                      CREATE TABLE test_table[id INT];\
                      INSERT INTO test_table VALUES[10];\
                      UPDATE test_table SET id=20 WHERE id=10;\
                      SELECT * FROM test_table;\
                      DROP TABLE test_table"];
if [status]
{
  printf["Could not execute statement[s]"];
  mysql_close[mysql];
  exit[0];
}

/* process each statement result */
do {
  /* did current statement return data? */
  result = mysql_store_result[mysql];
  if [result]
  {
    /* yes; process rows and free the result set */
    process_result_set[mysql, result];
    mysql_free_result[result];
  }
  else          /* no result set or error */
  {
    if [mysql_field_count[mysql] == 0]
    {
      printf["%lld rows affected\n",
            mysql_affected_rows[mysql]];
    }
    else  /* some error occurred */
    {
      printf["Could not retrieve result set\n"];
      break;
    }
  }
  /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
  if [[status = mysql_next_result[mysql]] > 0]
    printf["Could not execute statement\n"];
} while [status == 0];

mysql_close[mysql];
2, v.v.].

MySQL cũng hỗ trợ thực thi một chuỗi chứa nhiều câu lệnh được phân tách bằng các ký tự dấu chấm phẩy [

/* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
    db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
  printf["mysql_real_connect[] failed\n"];
  mysql_close[mysql];
  exit[1];
}

/* execute multiple statements */
status = mysql_query[mysql,
                     "DROP TABLE IF EXISTS test_table;\
                      CREATE TABLE test_table[id INT];\
                      INSERT INTO test_table VALUES[10];\
                      UPDATE test_table SET id=20 WHERE id=10;\
                      SELECT * FROM test_table;\
                      DROP TABLE test_table"];
if [status]
{
  printf["Could not execute statement[s]"];
  mysql_close[mysql];
  exit[0];
}

/* process each statement result */
do {
  /* did current statement return data? */
  result = mysql_store_result[mysql];
  if [result]
  {
    /* yes; process rows and free the result set */
    process_result_set[mysql, result];
    mysql_free_result[result];
  }
  else          /* no result set or error */
  {
    if [mysql_field_count[mysql] == 0]
    {
      printf["%lld rows affected\n",
            mysql_affected_rows[mysql]];
    }
    else  /* some error occurred */
    {
      printf["Could not retrieve result set\n"];
      break;
    }
  }
  /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
  if [[status = mysql_next_result[mysql]] > 0]
    printf["Could not execute statement\n"];
} while [status == 0];

mysql_close[mysql];
3]. Khả năng này được bật bởi các tùy chọn đặc biệt được chỉ định khi bạn kết nối với máy chủ với
/* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
    db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
  printf["mysql_real_connect[] failed\n"];
  mysql_close[mysql];
  exit[1];
}

/* execute multiple statements */
status = mysql_query[mysql,
                     "DROP TABLE IF EXISTS test_table;\
                      CREATE TABLE test_table[id INT];\
                      INSERT INTO test_table VALUES[10];\
                      UPDATE test_table SET id=20 WHERE id=10;\
                      SELECT * FROM test_table;\
                      DROP TABLE test_table"];
if [status]
{
  printf["Could not execute statement[s]"];
  mysql_close[mysql];
  exit[0];
}

/* process each statement result */
do {
  /* did current statement return data? */
  result = mysql_store_result[mysql];
  if [result]
  {
    /* yes; process rows and free the result set */
    process_result_set[mysql, result];
    mysql_free_result[result];
  }
  else          /* no result set or error */
  {
    if [mysql_field_count[mysql] == 0]
    {
      printf["%lld rows affected\n",
            mysql_affected_rows[mysql]];
    }
    else  /* some error occurred */
    {
      printf["Could not retrieve result set\n"];
      break;
    }
  }
  /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
  if [[status = mysql_next_result[mysql]] > 0]
    printf["Could not execute statement\n"];
} while [status == 0];

mysql_close[mysql];
4 hoặc sau khi kết nối bằng cách gọi
/* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
    db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
  printf["mysql_real_connect[] failed\n"];
  mysql_close[mysql];
  exit[1];
}

/* execute multiple statements */
status = mysql_query[mysql,
                     "DROP TABLE IF EXISTS test_table;\
                      CREATE TABLE test_table[id INT];\
                      INSERT INTO test_table VALUES[10];\
                      UPDATE test_table SET id=20 WHERE id=10;\
                      SELECT * FROM test_table;\
                      DROP TABLE test_table"];
if [status]
{
  printf["Could not execute statement[s]"];
  mysql_close[mysql];
  exit[0];
}

/* process each statement result */
do {
  /* did current statement return data? */
  result = mysql_store_result[mysql];
  if [result]
  {
    /* yes; process rows and free the result set */
    process_result_set[mysql, result];
    mysql_free_result[result];
  }
  else          /* no result set or error */
  {
    if [mysql_field_count[mysql] == 0]
    {
      printf["%lld rows affected\n",
            mysql_affected_rows[mysql]];
    }
    else  /* some error occurred */
    {
      printf["Could not retrieve result set\n"];
      break;
    }
  }
  /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
  if [[status = mysql_next_result[mysql]] > 0]
    printf["Could not execute statement\n"];
} while [status == 0];

mysql_close[mysql];
5.

Thực hiện một chuỗi nhiều statement có thể tạo ra nhiều bộ kết quả hoặc các chỉ báo số hàng. Xử lý các kết quả này liên quan đến một cách tiếp cận khác với trường hợp tuyên bố đơn: sau khi xử lý kết quả từ câu lệnh đầu tiên, cần kiểm tra xem có nhiều kết quả tồn tại hay không và lần lượt xử lý chúng nếu như vậy. Để hỗ trợ xử lý nhiều kết quả, C API bao gồm các hàm

/* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
    db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
  printf["mysql_real_connect[] failed\n"];
  mysql_close[mysql];
  exit[1];
}

/* execute multiple statements */
status = mysql_query[mysql,
                     "DROP TABLE IF EXISTS test_table;\
                      CREATE TABLE test_table[id INT];\
                      INSERT INTO test_table VALUES[10];\
                      UPDATE test_table SET id=20 WHERE id=10;\
                      SELECT * FROM test_table;\
                      DROP TABLE test_table"];
if [status]
{
  printf["Could not execute statement[s]"];
  mysql_close[mysql];
  exit[0];
}

/* process each statement result */
do {
  /* did current statement return data? */
  result = mysql_store_result[mysql];
  if [result]
  {
    /* yes; process rows and free the result set */
    process_result_set[mysql, result];
    mysql_free_result[result];
  }
  else          /* no result set or error */
  {
    if [mysql_field_count[mysql] == 0]
    {
      printf["%lld rows affected\n",
            mysql_affected_rows[mysql]];
    }
    else  /* some error occurred */
    {
      printf["Could not retrieve result set\n"];
      break;
    }
  }
  /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
  if [[status = mysql_next_result[mysql]] > 0]
    printf["Could not execute statement\n"];
} while [status == 0];

mysql_close[mysql];
6 và
/* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
    db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
  printf["mysql_real_connect[] failed\n"];
  mysql_close[mysql];
  exit[1];
}

/* execute multiple statements */
status = mysql_query[mysql,
                     "DROP TABLE IF EXISTS test_table;\
                      CREATE TABLE test_table[id INT];\
                      INSERT INTO test_table VALUES[10];\
                      UPDATE test_table SET id=20 WHERE id=10;\
                      SELECT * FROM test_table;\
                      DROP TABLE test_table"];
if [status]
{
  printf["Could not execute statement[s]"];
  mysql_close[mysql];
  exit[0];
}

/* process each statement result */
do {
  /* did current statement return data? */
  result = mysql_store_result[mysql];
  if [result]
  {
    /* yes; process rows and free the result set */
    process_result_set[mysql, result];
    mysql_free_result[result];
  }
  else          /* no result set or error */
  {
    if [mysql_field_count[mysql] == 0]
    {
      printf["%lld rows affected\n",
            mysql_affected_rows[mysql]];
    }
    else  /* some error occurred */
    {
      printf["Could not retrieve result set\n"];
      break;
    }
  }
  /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
  if [[status = mysql_next_result[mysql]] > 0]
    printf["Could not execute statement\n"];
} while [status == 0];

mysql_close[mysql];
7. Các chức năng này được sử dụng ở cuối một vòng lặp lặp đi lặp lại miễn là có nhiều kết quả hơn. Việc không xử lý kết quả theo cách này có thể dẫn đến kết nối bị rơi với máy chủ.Failure to process the result this way may result in a dropped connection to the server.

Xử lý nhiều kết quả cũng là bắt buộc nếu bạn thực hiện các câu lệnh

/* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
    db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
  printf["mysql_real_connect[] failed\n"];
  mysql_close[mysql];
  exit[1];
}

/* execute multiple statements */
status = mysql_query[mysql,
                     "DROP TABLE IF EXISTS test_table;\
                      CREATE TABLE test_table[id INT];\
                      INSERT INTO test_table VALUES[10];\
                      UPDATE test_table SET id=20 WHERE id=10;\
                      SELECT * FROM test_table;\
                      DROP TABLE test_table"];
if [status]
{
  printf["Could not execute statement[s]"];
  mysql_close[mysql];
  exit[0];
}

/* process each statement result */
do {
  /* did current statement return data? */
  result = mysql_store_result[mysql];
  if [result]
  {
    /* yes; process rows and free the result set */
    process_result_set[mysql, result];
    mysql_free_result[result];
  }
  else          /* no result set or error */
  {
    if [mysql_field_count[mysql] == 0]
    {
      printf["%lld rows affected\n",
            mysql_affected_rows[mysql]];
    }
    else  /* some error occurred */
    {
      printf["Could not retrieve result set\n"];
      break;
    }
  }
  /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
  if [[status = mysql_next_result[mysql]] > 0]
    printf["Could not execute statement\n"];
} while [status == 0];

mysql_close[mysql];
8 cho các thủ tục được lưu trữ. Kết quả từ một thủ tục được lưu trữ có những đặc điểm này:

  • Các câu lệnh trong quy trình có thể tạo ra các tập kết quả [ví dụ: nếu nó thực thi các câu lệnh SELECT]. Các bộ kết quả này được trả về theo thứ tự mà chúng được sản xuất khi thủ tục thực thi.

    Nói chung, người gọi không thể biết có bao nhiêu kết quả đặt một thủ tục sẽ trở lại. Thực hiện thủ tục có thể phụ thuộc vào các vòng lặp hoặc các câu lệnh có điều kiện khiến đường dẫn thực thi khác với cuộc gọi này sang cuộc gọi tiếp theo. Do đó, bạn phải chuẩn bị để lấy nhiều kết quả.

  • Kết quả cuối cùng từ thủ tục là kết quả trạng thái bao gồm không có kết quả. Trạng thái cho biết thủ tục thành công hay xảy ra lỗi.

Khả năng nhiều tuyên bố và kết quả chỉ có thể được sử dụng với mysql_real_query[] hoặc mysql_query[]. Chúng không thể được sử dụng với giao diện câu lệnh đã chuẩn bị. Trình xử lý câu lệnh được chuẩn bị được xác định chỉ hoạt động với các chuỗi có chứa một câu lệnh duy nhất. Xem Chương & NBSP; 6, C Giao diện câu lệnh đã chuẩn bị API.

Để cho phép thực hiện nhiều lần thực hiện và xử lý kết quả, các tùy chọn sau có thể được sử dụng:

  • Hàm

    /* connect to server with the CLIENT_MULTI_STATEMENTS option */
    if [mysql_real_connect [mysql, host_name, user_name, password,
        db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
    {
      printf["mysql_real_connect[] failed\n"];
      mysql_close[mysql];
      exit[1];
    }
    
    /* execute multiple statements */
    status = mysql_query[mysql,
                         "DROP TABLE IF EXISTS test_table;\
                          CREATE TABLE test_table[id INT];\
                          INSERT INTO test_table VALUES[10];\
                          UPDATE test_table SET id=20 WHERE id=10;\
                          SELECT * FROM test_table;\
                          DROP TABLE test_table"];
    if [status]
    {
      printf["Could not execute statement[s]"];
      mysql_close[mysql];
      exit[0];
    }
    
    /* process each statement result */
    do {
      /* did current statement return data? */
      result = mysql_store_result[mysql];
      if [result]
      {
        /* yes; process rows and free the result set */
        process_result_set[mysql, result];
        mysql_free_result[result];
      }
      else          /* no result set or error */
      {
        if [mysql_field_count[mysql] == 0]
        {
          printf["%lld rows affected\n",
                mysql_affected_rows[mysql]];
        }
        else  /* some error occurred */
        {
          printf["Could not retrieve result set\n"];
          break;
        }
      }
      /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
      if [[status = mysql_next_result[mysql]] > 0]
        printf["Could not execute statement\n"];
    } while [status == 0];
    
    mysql_close[mysql];
    4 có đối số CREATE TABLE3 trong đó hai giá trị tùy chọn có liên quan:

    • CREATE TABLE4 cho phép chương trình máy khách xử lý nhiều kết quả. Tùy chọn này phải được bật nếu bạn thực thi các câu lệnh

      /* connect to server with the CLIENT_MULTI_STATEMENTS option */
      if [mysql_real_connect [mysql, host_name, user_name, password,
          db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
      {
        printf["mysql_real_connect[] failed\n"];
        mysql_close[mysql];
        exit[1];
      }
      
      /* execute multiple statements */
      status = mysql_query[mysql,
                           "DROP TABLE IF EXISTS test_table;\
                            CREATE TABLE test_table[id INT];\
                            INSERT INTO test_table VALUES[10];\
                            UPDATE test_table SET id=20 WHERE id=10;\
                            SELECT * FROM test_table;\
                            DROP TABLE test_table"];
      if [status]
      {
        printf["Could not execute statement[s]"];
        mysql_close[mysql];
        exit[0];
      }
      
      /* process each statement result */
      do {
        /* did current statement return data? */
        result = mysql_store_result[mysql];
        if [result]
        {
          /* yes; process rows and free the result set */
          process_result_set[mysql, result];
          mysql_free_result[result];
        }
        else          /* no result set or error */
        {
          if [mysql_field_count[mysql] == 0]
          {
            printf["%lld rows affected\n",
                  mysql_affected_rows[mysql]];
          }
          else  /* some error occurred */
          {
            printf["Could not retrieve result set\n"];
            break;
          }
        }
        /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
        if [[status = mysql_next_result[mysql]] > 0]
          printf["Could not execute statement\n"];
      } while [status == 0];
      
      mysql_close[mysql];
      8 cho các quy trình được lưu trữ tạo ra các bộ kết quả. Mặt khác, các quy trình đó dẫn đến lỗi CREATE TABLE6 không thể trả về kết quả được đặt trong bối cảnh đã cho. CREATE TABLE4 được bật theo mặc định.must be enabled if you execute
      /* connect to server with the CLIENT_MULTI_STATEMENTS option */
      if [mysql_real_connect [mysql, host_name, user_name, password,
          db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
      {
        printf["mysql_real_connect[] failed\n"];
        mysql_close[mysql];
        exit[1];
      }
      
      /* execute multiple statements */
      status = mysql_query[mysql,
                           "DROP TABLE IF EXISTS test_table;\
                            CREATE TABLE test_table[id INT];\
                            INSERT INTO test_table VALUES[10];\
                            UPDATE test_table SET id=20 WHERE id=10;\
                            SELECT * FROM test_table;\
                            DROP TABLE test_table"];
      if [status]
      {
        printf["Could not execute statement[s]"];
        mysql_close[mysql];
        exit[0];
      }
      
      /* process each statement result */
      do {
        /* did current statement return data? */
        result = mysql_store_result[mysql];
        if [result]
        {
          /* yes; process rows and free the result set */
          process_result_set[mysql, result];
          mysql_free_result[result];
        }
        else          /* no result set or error */
        {
          if [mysql_field_count[mysql] == 0]
          {
            printf["%lld rows affected\n",
                  mysql_affected_rows[mysql]];
          }
          else  /* some error occurred */
          {
            printf["Could not retrieve result set\n"];
            break;
          }
        }
        /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
        if [[status = mysql_next_result[mysql]] > 0]
          printf["Could not execute statement\n"];
      } while [status == 0];
      
      mysql_close[mysql];
      8 statements for stored procedures that produce result sets. Otherwise, such procedures result in an error CREATE TABLE6 can't return a result set in the given context. CREATE TABLE4 is enabled by default.

    • CREATE TABLE8 cho phép mysql_real_query[]mysql_query[] thực thi các chuỗi câu lệnh chứa nhiều câu lệnh được phân tách bằng dấu chấm phẩy. Tùy chọn này cũng cho phép CREATE TABLE4 ngầm, do đó, một đối số CREATE TABLE3 của CREATE TABLE8 với

      /* connect to server with the CLIENT_MULTI_STATEMENTS option */
      if [mysql_real_connect [mysql, host_name, user_name, password,
          db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
      {
        printf["mysql_real_connect[] failed\n"];
        mysql_close[mysql];
        exit[1];
      }
      
      /* execute multiple statements */
      status = mysql_query[mysql,
                           "DROP TABLE IF EXISTS test_table;\
                            CREATE TABLE test_table[id INT];\
                            INSERT INTO test_table VALUES[10];\
                            UPDATE test_table SET id=20 WHERE id=10;\
                            SELECT * FROM test_table;\
                            DROP TABLE test_table"];
      if [status]
      {
        printf["Could not execute statement[s]"];
        mysql_close[mysql];
        exit[0];
      }
      
      /* process each statement result */
      do {
        /* did current statement return data? */
        result = mysql_store_result[mysql];
        if [result]
        {
          /* yes; process rows and free the result set */
          process_result_set[mysql, result];
          mysql_free_result[result];
        }
        else          /* no result set or error */
        {
          if [mysql_field_count[mysql] == 0]
          {
            printf["%lld rows affected\n",
                  mysql_affected_rows[mysql]];
          }
          else  /* some error occurred */
          {
            printf["Could not retrieve result set\n"];
            break;
          }
        }
        /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
        if [[status = mysql_next_result[mysql]] > 0]
          printf["Could not execute statement\n"];
      } while [status == 0];
      
      mysql_close[mysql];
      4 tương đương với một đối số của INSERT INTO5. Đó là, CREATE TABLE8 là đủ để cho phép thực thi nhiều câu chuyện và tất cả các xử lý nhiều kết quả.

  • Sau khi kết nối với máy chủ đã được thiết lập, bạn có thể sử dụng chức năng

    /* connect to server with the CLIENT_MULTI_STATEMENTS option */
    if [mysql_real_connect [mysql, host_name, user_name, password,
        db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
    {
      printf["mysql_real_connect[] failed\n"];
      mysql_close[mysql];
      exit[1];
    }
    
    /* execute multiple statements */
    status = mysql_query[mysql,
                         "DROP TABLE IF EXISTS test_table;\
                          CREATE TABLE test_table[id INT];\
                          INSERT INTO test_table VALUES[10];\
                          UPDATE test_table SET id=20 WHERE id=10;\
                          SELECT * FROM test_table;\
                          DROP TABLE test_table"];
    if [status]
    {
      printf["Could not execute statement[s]"];
      mysql_close[mysql];
      exit[0];
    }
    
    /* process each statement result */
    do {
      /* did current statement return data? */
      result = mysql_store_result[mysql];
      if [result]
      {
        /* yes; process rows and free the result set */
        process_result_set[mysql, result];
        mysql_free_result[result];
      }
      else          /* no result set or error */
      {
        if [mysql_field_count[mysql] == 0]
        {
          printf["%lld rows affected\n",
                mysql_affected_rows[mysql]];
        }
        else  /* some error occurred */
        {
          printf["Could not retrieve result set\n"];
          break;
        }
      }
      /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
      if [[status = mysql_next_result[mysql]] > 0]
        printf["Could not execute statement\n"];
    } while [status == 0];
    
    mysql_close[mysql];
    5 để bật hoặc tắt thực thi nhiều lần bằng cách chuyển cho nó một đối số là INSERT INTO8 hoặc INSERT INTO9. Kích hoạt thực thi nhiều tuyên bố với chức năng này cũng cho phép xử lý các kết quả đơn giản cho một chuỗi nhiều câu trong đó mỗi câu lệnh tạo ra một kết quả duy nhất, nhưng không đủ để cho phép xử lý các quy trình được lưu trữ tạo ra các bộ kết quả.simple results for a multiple-statement string where each statement produces a single result, but is not sufficient to permit processing of stored procedures that produce result sets.

Quy trình sau đây phác thảo một chiến lược được đề xuất để xử lý nhiều tuyên bố:

  1. Vượt qua CREATE TABLE8 cho

    /* connect to server with the CLIENT_MULTI_STATEMENTS option */
    if [mysql_real_connect [mysql, host_name, user_name, password,
        db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
    {
      printf["mysql_real_connect[] failed\n"];
      mysql_close[mysql];
      exit[1];
    }
    
    /* execute multiple statements */
    status = mysql_query[mysql,
                         "DROP TABLE IF EXISTS test_table;\
                          CREATE TABLE test_table[id INT];\
                          INSERT INTO test_table VALUES[10];\
                          UPDATE test_table SET id=20 WHERE id=10;\
                          SELECT * FROM test_table;\
                          DROP TABLE test_table"];
    if [status]
    {
      printf["Could not execute statement[s]"];
      mysql_close[mysql];
      exit[0];
    }
    
    /* process each statement result */
    do {
      /* did current statement return data? */
      result = mysql_store_result[mysql];
      if [result]
      {
        /* yes; process rows and free the result set */
        process_result_set[mysql, result];
        mysql_free_result[result];
      }
      else          /* no result set or error */
      {
        if [mysql_field_count[mysql] == 0]
        {
          printf["%lld rows affected\n",
                mysql_affected_rows[mysql]];
        }
        else  /* some error occurred */
        {
          printf["Could not retrieve result set\n"];
          break;
        }
      }
      /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
      if [[status = mysql_next_result[mysql]] > 0]
        printf["Could not execute statement\n"];
    } while [status == 0];
    
    mysql_close[mysql];
    4, để kích hoạt hoàn toàn thực thi nhiều trường hợp và xử lý nhiều kết quả.

  2. Sau khi gọi mysql_real_query[] hoặc mysql_query[] và xác minh rằng nó thành công, hãy nhập một vòng lặp trong đó bạn xử lý kết quả báo cáo.

  3. Đối với mỗi lần lặp của vòng lặp, xử lý kết quả câu lệnh hiện tại, truy xuất một tập kết quả hoặc số lượng hàng bị ảnh hưởng. Nếu xảy ra lỗi, thoát khỏi vòng lặp.

  4. Vào cuối vòng lặp, hãy gọi

    /* connect to server with the CLIENT_MULTI_STATEMENTS option */
    if [mysql_real_connect [mysql, host_name, user_name, password,
        db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
    {
      printf["mysql_real_connect[] failed\n"];
      mysql_close[mysql];
      exit[1];
    }
    
    /* execute multiple statements */
    status = mysql_query[mysql,
                         "DROP TABLE IF EXISTS test_table;\
                          CREATE TABLE test_table[id INT];\
                          INSERT INTO test_table VALUES[10];\
                          UPDATE test_table SET id=20 WHERE id=10;\
                          SELECT * FROM test_table;\
                          DROP TABLE test_table"];
    if [status]
    {
      printf["Could not execute statement[s]"];
      mysql_close[mysql];
      exit[0];
    }
    
    /* process each statement result */
    do {
      /* did current statement return data? */
      result = mysql_store_result[mysql];
      if [result]
      {
        /* yes; process rows and free the result set */
        process_result_set[mysql, result];
        mysql_free_result[result];
      }
      else          /* no result set or error */
      {
        if [mysql_field_count[mysql] == 0]
        {
          printf["%lld rows affected\n",
                mysql_affected_rows[mysql]];
        }
        else  /* some error occurred */
        {
          printf["Could not retrieve result set\n"];
          break;
        }
      }
      /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
      if [[status = mysql_next_result[mysql]] > 0]
        printf["Could not execute statement\n"];
    } while [status == 0];
    
    mysql_close[mysql];
    7 để kiểm tra xem một kết quả khác có tồn tại và bắt đầu truy xuất cho nó không. Nếu không có thêm kết quả, hãy thoát khỏi vòng lặp.

Một thực hiện có thể của chiến lược trước được hiển thị sau. Phần cuối cùng của vòng lặp có thể được giảm xuống thành một bài kiểm tra đơn giản về việc

/* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
    db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
  printf["mysql_real_connect[] failed\n"];
  mysql_close[mysql];
  exit[1];
}

/* execute multiple statements */
status = mysql_query[mysql,
                     "DROP TABLE IF EXISTS test_table;\
                      CREATE TABLE test_table[id INT];\
                      INSERT INTO test_table VALUES[10];\
                      UPDATE test_table SET id=20 WHERE id=10;\
                      SELECT * FROM test_table;\
                      DROP TABLE test_table"];
if [status]
{
  printf["Could not execute statement[s]"];
  mysql_close[mysql];
  exit[0];
}

/* process each statement result */
do {
  /* did current statement return data? */
  result = mysql_store_result[mysql];
  if [result]
  {
    /* yes; process rows and free the result set */
    process_result_set[mysql, result];
    mysql_free_result[result];
  }
  else          /* no result set or error */
  {
    if [mysql_field_count[mysql] == 0]
    {
      printf["%lld rows affected\n",
            mysql_affected_rows[mysql]];
    }
    else  /* some error occurred */
    {
      printf["Could not retrieve result set\n"];
      break;
    }
  }
  /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
  if [[status = mysql_next_result[mysql]] > 0]
    printf["Could not execute statement\n"];
} while [status == 0];

mysql_close[mysql];
7 có trả lại không khác. Mã bằng văn bản phân biệt giữa không còn kết quả và lỗi, cho phép một thông báo được in cho lần xuất hiện sau.

/* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
    db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
  printf["mysql_real_connect[] failed\n"];
  mysql_close[mysql];
  exit[1];
}

/* execute multiple statements */
status = mysql_query[mysql,
                     "DROP TABLE IF EXISTS test_table;\
                      CREATE TABLE test_table[id INT];\
                      INSERT INTO test_table VALUES[10];\
                      UPDATE test_table SET id=20 WHERE id=10;\
                      SELECT * FROM test_table;\
                      DROP TABLE test_table"];
if [status]
{
  printf["Could not execute statement[s]"];
  mysql_close[mysql];
  exit[0];
}

/* process each statement result */
do {
  /* did current statement return data? */
  result = mysql_store_result[mysql];
  if [result]
  {
    /* yes; process rows and free the result set */
    process_result_set[mysql, result];
    mysql_free_result[result];
  }
  else          /* no result set or error */
  {
    if [mysql_field_count[mysql] == 0]
    {
      printf["%lld rows affected\n",
            mysql_affected_rows[mysql]];
    }
    else  /* some error occurred */
    {
      printf["Could not retrieve result set\n"];
      break;
    }
  }
  /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
  if [[status = mysql_next_result[mysql]] > 0]
    printf["Could not execute statement\n"];
} while [status == 0];

mysql_close[mysql];

MySQL có thể chạy hai truy vấn cùng một lúc không?

MySQL tùy chọn cho phép có nhiều câu lệnh trong một chuỗi câu lệnh, nhưng nó yêu cầu xử lý đặc biệt. Nhiều câu lệnh hoặc nhiều truy vấn phải được thực thi với mysqli :: multi_query []. Các câu lệnh riêng lẻ của chuỗi câu lệnh được phân tách bằng dấu chấm phẩy.. Multiple statements or multi queries must be executed with mysqli::multi_query[]. The individual statements of the statement string are separated by semicolon.

Làm thế nào tôi có thể viết hai truy vấn trong mysql?

Nhà điều hành Liên minh MySQL Để kết hợp bộ kết quả của hai hoặc nhiều truy vấn bằng toán tử Union, đây là những quy tắc cơ bản mà bạn phải tuân theo: Đầu tiên, số và đơn đặt hàng của các cột xuất hiện trong tất cả các câu lệnh được chọn phải giống nhau.Thứ hai, các loại dữ liệu của các cột phải giống nhau hoặc tương thích.the number and the orders of columns that appear in all SELECT statements must be the same. Second, the data types of columns must be the same or compatible.

Bạn có thể sử dụng 2 câu lệnh Chọn trong SQL không?

Đặt khác nhau, Union cho phép bạn viết hai câu lệnh chọn riêng biệt và để có kết quả của một câu lệnh hiển thị trong cùng một bảng với kết quả từ câu lệnh khác.SQL có các quy tắc nghiêm ngặt để nối thêm dữ liệu: cả hai bảng phải có cùng một số cột.UNION allows you to write two separate SELECT statements, and to have the results of one statement display in the same table as the results from the other statement. SQL has strict rules for appending data: Both tables must have the same number of columns.

Bạn có thể tham gia 2 truy vấn không?

Đôi khi bạn có thể muốn liệt kê các bản ghi từ một bảng hoặc truy vấn với các bảng từ một hoặc nhiều bảng khác để tạo thành một bộ hồ sơ - một danh sách với tất cả các bản ghi từ hai hoặc nhiều bảng.Đây là mục đích của một truy vấn công đoàn trong truy cập.

Bài Viết Liên Quan

Chủ Đề