如何选择一个表的所有列,除了一个列?

如何选择一个表的所有列,除了一个列?

我有近259栏,我不能提到258栏在 SELECT语句。

还有别的办法吗?

323121 次浏览

You can use this approach to get the data from all the columns except one:-

  1. Insert all the data into a temporary table
  2. Then drop the column which you dont want from the temporary table
  3. Fetch the data from the temporary table(This will not contain the data of the removed column)
  4. Drop the temporary table

Something like this:

SELECT * INTO #TemporaryTable FROM YourTableName


ALTER TABLE #TemporaryTable DROP COLUMN Columnwhichyouwanttoremove


SELECT * FROM #TemporaryTable


DROP TABLE #TemporaryTable

There are lot of options available , one of them is :

 CREATE TEMPORARY TABLE temp_tb SELECT * FROM orig_tb;
ALTER TABLE temp_tb DROP col_x;
SELECT * FROM temp_tb;

Here the col_x is the column which u dont want to include in select statement.

Take a look at this question : Select all columns except one in MySQL?

Create a view. Yes, in the view creation statement, you will have to list each...and...every...field...by...name.

Once.

Then just select * from viewname after that.

You can get the column name details from sys.columns table

Try the following query:

SELECT * FROM SYS.COLUMNS
WHERE object_id = OBJECT_ID('dbo.TableName')
AND [Name] <> 'ColumnName'


DECLARE @sql as VARCHAR(8000)
SET @sql = 'SELECT '


SELECT @sql += [Name] + ', ' FROM SYS.COLUMNS
WHERE object_id = OBJECT_ID('dbo.TableName')
AND [Name] <> 'ColumnName'


SELECT @sql += ' FROM Dbo.TableName'


EXEC(@sql)

Without creating new table you can do simply (e.g with mysqli):

  1. get all columns
  2. loop through all columns and remove wich you want
  3. make your query

$r = mysqli_query('SELECT column_name FROM information_schema.columns WHERE table_name = table_to_query');


$c = count($r); while($c--) if($r[$c]['column_name'] != 'column_to_remove_from_query') $a[] = $r[$c]['column_name']; else unset($r[$c]);


$r = mysqli_query('SELECT ' . implode(',', $a) . ' FROM table_to_query');

In your case, expand columns of that database in the object explorer. Drag the columns in to the query area.

And then just delete one or two columns which you don't want and then run it. I'm open to any suggestions easier than this.

Try the following query:

DECLARE @Temp NVARCHAR(MAX);
DECLARE @SQL NVARCHAR(MAX);


SET @Temp = '';
SELECT @Temp = @Temp + COLUMN_NAME + ', ' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ='Person' AND COLUMN_NAME NOT IN ('Id')


SET @SQL = 'SELECT ' + SUBSTRING(@Temp, 0, LEN(@Temp)) +' FROM [Person]';
EXECUTE SP_EXECUTESQL @SQL;

Only one way to achieve this giving column name. There is no other method found. You must have to list all column name

This is not a generic solution, but some databases allow you to use regular expressions to specify the columns.

For instance, in the case of Hive, the following query selects all columns except ds and hr:

SELECT `(ds|hr)?+.+` FROM sales

I just wanted to echo @Luann's comment as I use this approach always.

Just right click on the table > Script table as > Select to > New Query window.

You will see the select query. Just take out the column you want to exclude and you have your preferred select query. enter image description here

If you are using DataGrip you can do the following:

  1. Enter your SELECT statement SELECT * FROM <your_table>;
  2. Put your cursor over * and press Alt+Enter
  3. You will get pop up menu with Expand column list option
  4. Click on it and it will convert * with full list of columns
  5. Now you can remove columns that you don't need

Here is a link for an example on how to do it.

You can retrieve the list of column name by simple query and then remove those column by apply where query like this.

SELECT * FROM (
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'TableName'
) AS allColumns
WHERE allColumns.COLUMN_NAME NOT IN ('unwantedCol1', 'unwantedCol2')