Select all columns except one in MySQL?
Actually there is a way, you need to have permissions of course for doing this ...
SET @sql = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), '<columns_to_omit>,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '<table>' AND TABLE_SCHEMA = '<database>'), ' FROM <table>');
PREPARE stmt1 FROM @sql;
EXECUTE stmt1;
Replacing <table>, <database> and <columns_to_omit>
Selecting all fields except only one field in mysql
you can do it easily like that
lets say your field is an id = 5
then
select * from your_table where id !=5
and if you mean columns
lets say you dont want select column3
then
select column1,column2,column4 from tablename;
if you have many columns
SET @sql = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), '<columns_to_delete>,', '')
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '<table>' AND TABLE_SCHEMA = '<database>'), ' FROM <table>');
PREPARE stmt1 FROM @sql;
EXECUTE stmt1;
How to select all the columns of a table except one column?
You can use this approach to get the data from all the columns except one:-
- Insert all the data into a temporary table
- Then drop the column which you dont want from the temporary table
- Fetch the data from the temporary table(This will not contain the data of the removed column)
- Drop the temporary table
Something like this:
SELECT * INTO #TemporaryTable FROM YourTableName
ALTER TABLE #TemporaryTable DROP COLUMN Columnwhichyouwanttoremove
SELECT * FROM #TemporaryTable
DROP TABLE #TemporaryTable
How to select all columns in sql except one column?
declare @cols varchar(max), @sql varchar(max)
SELECT @cols = STUFF
(
(
SELECT DISTINCT '], [' + name
FROM sys.columns
where object_id = (
select top 1 object_id from sys.objects
where name = 'TBLUser'
)
and name not in ('age')
FOR XML PATH('')
), 1, 2, ''
) + ']'
select @sql = 'select ' + @cols + ' from TBLUser'
exec (@sql)
Select all columns except one column in CakePHP?
I doubt you will gain much by excluding the column from your query. If you really need to do this then you can get the model's schema and then remove the column from that and then use the remaining columns in your find()
by defining the fields you want returned by the query:-
// Get the model’s schema.
$schema = $this->Order->schema();
// Remove the `attribute` column from the schema.
unset($schema['attribute']);
// Determine the remaining columns from the schema.
$cols = array_keys($schema);
// Now call your query specifying the fields you want back using the
// columns we’ve just determined.
$data = $this->Order->find('first',
array(
'fields' => $cols,
'conditions' => array('Order.id' => 1)
)
);
Select all columns except one in MySQL?
Actually there is a way, you need to have permissions of course for doing this ...
SET @sql = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), '<columns_to_omit>,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '<table>' AND TABLE_SCHEMA = '<database>'), ' FROM <table>');
PREPARE stmt1 FROM @sql;
EXECUTE stmt1;
Replacing <table>, <database> and <columns_to_omit>
MySQL: Selecting all columns in a table plus one column from the same table
You have to give the table name in your query, otherwise mysql complains :
SELECT column20, mytable.* FROM mytable
PS: I have absolutely no idea as to why, because SELECT *, column20 FROM mytable
works just fine... Strange things happens sometimes ^^
Related Topics
Convert to Date Format Dd/Mm/Yyyy
Try Catch Cannot Work with Require_Once in PHP
What Is 22527 in Error_Reporting 22527 of PHPinfo
Curl Requires Curlopt_Ssl_Verifypeer=False
How to Search Through a JSON Array in PHP
Copy File from Remote Server or Url
PHP Session Ids -- How Are They Generated
Php: Catch Exception and Continue Execution, Is It Possible
PHP Artisan Migrate Throwing [Pdo Exception] Could Not Find Driver - Using Laravel
Symfony2: How to Access Service from Template
Array_Unique and Then Renumbering Keys
How to Employ "If Exists" for Creating or Dropping an Index in MySQL
Www to Non-Www Redirect with PHP
Magento - Load Only Configurable Products
Can Anyone Get Access to My PHP Source Code
How to Access a Different Controller from Inside a Controller Symfony2