How to choose MySQL in all columns except one outside

By SQL SELECT * (SELECT ALL) clause so that all field selection table becomes very simple. Unfortunately, once you delete one from the list, SELECT ALL statement will disappear. Write the name of each column quickly becomes tedious, especially if you happen to be processed table contains many columns. If we can choose for each of the columns except one - by excluding rather than including selection, then what will happen? It can be done. In fact, there are two ways to do this - a simple, another a little simpler.

Here are two methods in detail:

Method 1: Use Table INFORMATION_SCHEMA.COLUMNS

INFORMATION_SCHEMA provides access to database metadata information (such as a database or table name, column data type or access rights) relating to MySQL server. More specifically, COLUMNS table provides information about the table columns, including the column name.

Number of columns in the table Sakila film sample database contains at most 13.

Navicat Tutorial: How to choose MySQL in all columns except one outside

This is our way of using INFORMATION_SCHEMA.COLUMNS table to get all the columns except original_language_id columns:

Navicat Tutorial: How to choose MySQL in all columns except one outside

GROUP_CONCAT function connected to the string names of all the columns in a comma-separated. Then, we can replace the field empty string!

Execute the query

A small obstacle to overcome is the MySQL query does not accept dynamic column name. Solution is to use prepared statement. The following is a set @sql variables, prepared statements and execute code:

Navicat Tutorial: How to choose MySQL in all columns except one outside

Insert a column in a query, and mode information table will produce the results we want:

Navicat Tutorial: How to choose MySQL in all columns except one outside

Method 2: Use Navicat

Navicat main goals of database development and management tools to improve productivity. Therefore, Navicat is designed to make your work as quickly and easily. To this end, with the code segment "Code Completion" and customizable, SQL Editor helps you write code faster, these code segments may provide suggestions for your keyword, and eliminate duplicate code. If this were not enough, Navicat also provides a useful tool called Query Builder to build queries visually. It allows you to use only a little knowledge of SQL can create and edit queries. Although Query Builder is mainly sold to more novice programmers, but people can still be proficient in SQL complete certain tasks in the Query Builder. One such task is to select the column.

In the query builder, a check box next to the name of the table, to select all columns. If you click it, you can simply uncheck original_language_id field to remove it from the column list:

Navicat Tutorial: How to choose MySQL in all columns except one outside

Click the OK button, then close the dialog box and SQL code to the editor:

Navicat Tutorial: How to choose MySQL in all columns except one outside

Compared with writing code by hand, using the Query Builder to create a query that has a number of advantages:

  • It minimizes input errors

  • It generates easy-to-read format SQL

in conclusion

In today's article, we learned several techniques to select each column of the table, but can only choose one or two.

Guess you like

Origin blog.51cto.com/14467432/2474074