如何从命令行清除未执行的 MySQL 查询?

假设您正在向 MySQL 数据库中输入命令行查询,并且需要取消并重新开始。在 bash shell 中,只需键入 ctrl-c,就可以得到一个新的提示符。在 MySQL 中,ctrl-c 将退出客户机并返回到 shell。

例如,我输入了一个长的、复杂的 SELECT 语句,但是我还没有点击 return。我意识到我不想发送这个命令,但是我想把这个命令放在屏幕上,这样我就可以把它作为参考。我不想退出 MySQL。有什么想法吗?

重点: 命令还没有执行

44586 次浏览

Type \c.

When you start up MySQL, you'll likely see this message:

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

The "buffer" that it's referring to is the command/query buffer.

If it's a long command I usually arrow to the beginning of the command and add gibberish, so it won't execute, but I can up-arrow and gain access without having to re-type it. Otherwise if you want to not execute it, just hit the up-arrow and it should scroll you through your command history. Find a short command, backspace, then type whatever you want.

Depends on your shell's key bindings. You could press home or what ever key sequence you use to get back to the start, prefix your query with X or what ever to make it syntactically invalid, hit enter and you're good

First type Ctrl+a, then Ctrl+k.

Use one of the following shortcuts to delete the current line:

  • Type Ctrl + u to delete everything from the cursor back to the line start
  • Type Ctrl + k to delete everything from the cursor to the end of the line

Otherwise as already indicated type \c at the end of the current line (a shortcut for the clear command) and then press Enter.

If you came here hoping to find the same answer in Transact-SQL, type RESET

In mysql 5.7, this has been resolved. You can now do ctrl-c to exit the SQL command line.