Column names from MySQL table in the right order - mysql

I am using the following SQL command to get all column names from my table in MySQL:
SELECT column_name FROM information_schema.columns where table_name = 'сведения о фильме' and table_schema = 'videokassety2';
It give me all column names in the table 'сведения о фильме', however it sorts the results and gives me the following:
ID Компании
Год выпуска
Название фильма
Номер фильма
Основные исполнители
Характер фильма
However, I do not need sorting, I need the order in which they appear in the table itself, like the following:
enter image description here
How can I get a list of column_nmaes without any sorting?
Thank you

The column ordinal_position has the original column ordering information:
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'сведения о фильме' AND table_schema = 'videokassety2'
ORDER BY ordinal_position;

Related

How to get last column name from mysql table?

I want to retrieve the last column name from a mysql table.
For example the schema would look like this:
TABLE example {surname,firstname,birthdate}
In this example I want to get the column name "birthdate" from table "example".
How do I achieve this in MySQL?
Please try this:
SELECT
COLUMN_NAME,
ORDINAL_POSITION
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'YOUR_DATABASE_NAME'
AND TABLE_NAME ='YOUR_TABLE_NAME'
ORDER BY ORDINAL_POSITION DESC
LIMIT 1;
Information_schema.columns stores column specific information.
Also try this one, The Solution from command line mysql
Learn about Information Schema
mysql>USE information_schema;
mysql>SELECT COLUMN_NAME,ORDINAL_POSITION FROM COLUMNS WHERE TABLE_SCHEMA = '<--DATABASE_NAME-->' AND TABLE_NAME='<--TABLENAME-->' ORDER BY ORDINAL_POSITION desc limit 1

Search columns in mysql table?

I have a table with 300+ column. Looking for a specific column is like nightmare. Is there any query where If I would like to search for columns stats with 'grand' can be listed...
You can use show columns from table with where. Try the following query,
SHOW COLUMNS FROM tablename WHERE field like 'grand%';
Just put in your tablename after FROM and it would work.
select * from myTable where mycolumn like 'grand%'
Try this.
SELECT
table_name,
column_name,
data_type,
ordinal_position
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'DatabaseName' --- the database you want to search
AND table_name = 'yourTableName'
AND column_name LIKE '%Grand' ;
This one worked for me. Its shows all columns and table in entire database
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name OR table_name LIKE '%sale%';

Can you sort the output of SHOW COLUMNS or DESC?

Is it possible to (easily) sort the output of the SHOW COLUMNS or DESC MySQL Command?
you can sort the table names using order by but you have to select the column names from the information_schema below is the column names in alphabetical order
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'table_name' ORDER BY column_name

How to get column names with where condition included?

I am trying to use :
SHOW COLUMNS FROM #__tablename WHERE Field.columnname = 'any value' ;
If I use it without where condition then it retrieves all column names but I want to fetch specified column name.
Please suggest.
You can use information_schema.columns table instead of SHOW COLUMNS statement. The information_schema.columns table is the same as SHOW COLUMNS, but you can work with result-set as with ordinary table. For example -
SELECT * FROM information_schema.columns
WHERE table_schema = 'table name' AND table_name = 'table name';
Specify columns you need and WHERE filter.
You Can use information_schema.columns , and add a filter to fetch column you want, I think it works
SELECT * FROM information_schema.columns
WHERE table_schema = 'table name'
AND table_name = 'table name'
AND column_name = 'Column name'

Arrange fields of table in database in alphabetical order

I have a database "portal" and table "employee" and more than 60 fields in that table not arranged in alphabetical order. I want to arrange the fields name in ascending order from phpmyadmin or by any other means.
I tried MySQL sorting table by column names
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = '[portal]'
AND table_name = '[employee]'
ORDER BY column_name
...but it doesn't work.
You should be able to use the following:
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'portal'
AND table_name = 'employee'
ORDER BY column_name