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
Related
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;
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%';
As the question title suggests, I want to find the table having maximum number of rows (entries) in a particular database. I have been able to extract the names of all the tables in a particular database using the query below.
SELECT TABLE_NAME
FROM information_schema.tables
WHERE TABLE_SCHEMA="Some_Database";
How do I proceed beyond this?? I have been trying to formulate a nested query for the above purpose but couldn't come up with something (I am not very comfortable with them). Please Help.
EDIT: As given in this link the table_rows field does not give an accurate result. That is why I need to do something like a MAX (Count(*)) for each table.
Try this one......
SELECT TABLE_NAME,MAX(TABLE_ROWS)
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "DB_Name";
OR
please try the following two queries for actual result.
query 1:
SELECT CONCAT('SELECT COUNT(*) as cnt FROM ', table_name, ' union all')
FROM information_schema.tables WHERE table_schema = 'your_db_name';
query 2:
select max(cnt) from (paste the result of first query and remove
last union all keyword) as tmptable;
What about this:
SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "Some_Database"
ORDER BY TABLE_ROWS DESC
LIMIT 1;
information_schema.tables has a column named table_rows, so:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'Some_Database'
ORDER BY table_rows DESC
LIMIT 1;
We can obtain the table name having max number of rows in MySQL using the this query
SELECT
TABLE_NAME,MAX(TABLE_ROWS)
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'DB_Name'
GROUP BY TABLE_NAME ORDER BY MAX(TABLE_ROWS) DESC LIMIT 1;
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
Is there a way to sort the list of tables returned by mysql's 'show tables' command?
mysql> show tables;
I'd like to sort alphabetically by the table name.
EDIT:
As pointed out by one of the answers, they are already in alphabetical order. However, A != a. Is there a way to ignore case in the sort?
Query information_schema and replace database_name with the name of the database you want to return the tables from
SELECT table_name, engine
FROM information_schema.tables
WHERE table_type = 'BASE TABLE' AND table_schema='database_name'
ORDER BY table_name ASC;
They are already in alphabetical order!
SELECT CONCAT(`table_name`, '')
FROM information_schema.tables
order by 1 asc
All you need, just transform the table_name to regular varchar type. And then order it as usual string.
Please try this one and replace database name accordingly.
SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_schema =
'database_name' ORDER BY table_name ASC;