Arrange fields of table in database in alphabetical order - mysql

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

Related

Column names from MySQL table in the right order

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;

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 can I find all the tables in MySQL with specific column names in them?

I have 2-3 different column names that I want to look up in the entire database and list out all tables which have those columns. Is there any easy script?
To get all tables with columns columnA or ColumnB in the database YourDatabase:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('columnA','ColumnB')
AND TABLE_SCHEMA='YourDatabase';
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%wild%';
More simply done in one line of SQL:
SELECT * FROM information_schema.columns WHERE column_name = 'column_name';
SELECT DISTINCT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name LIKE 'employee%'
AND TABLE_SCHEMA='YourDatabase'
In older MySQL versions or some MySQL NDB Cluster versions that do not have information_schema, you can dump the table structure and search the column manually.
mysqldump -h$host -u$user -p$pass --compact --no-data --all-databases > some_file.sql
Now search the column name in some_file.sql using your preferred text editor, or use some nifty AWK scripts.
And a simple sed script to find the column. Just replace COLUMN_NAME with yours:
sed -n '/^USE/{h};/^CREATE/{H;x;s/\nCREATE.*\n/\n/;x};/COLUMN_NAME/{x;p};' <some_file.sql
USE `DATABASE_NAME`;
CREATE TABLE `TABLE_NAME` (
`COLUMN_NAME` varchar(10) NOT NULL,
You can pipe the dump directly in sed, but that's trivial.
For those searching for the inverse of this, i.e. looking for tables that do not contain a certain column name, here is the query...
SELECT DISTINCT TABLE_NAME FROM information_schema.columns WHERE
TABLE_SCHEMA = 'your_db_name' AND TABLE_NAME NOT IN (SELECT DISTINCT
TABLE_NAME FROM information_schema.columns WHERE column_name =
'column_name' AND TABLE_SCHEMA = 'your_db_name');
This came in really handy when we began to slowly implement use of InnoDB's special ai_col column and needed to figure out which of our 200 tables had yet to be upgraded.
Use this one line query. Replace desired_column_name by your column name.
SELECT TABLE_NAME FROM information_schema.columns WHERE column_name = 'desired_column_name';
If you want to "get all tables only", then use this query:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME like '%'
and TABLE_SCHEMA = 'tresbu_lk'
If you want "to get all tables with columns", then use this query:
SELECT DISTINCT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name LIKE '%'
AND TABLE_SCHEMA='tresbu_lk'
select distinct table_name
from information_schema.columns
where column_name in ('ColumnA')
and table_schema='YourDatabase';
and table_name in
(
select distinct table_name
from information_schema.columns
where column_name in ('ColumnB')
and table_schema='YourDatabase';
);
That ^^ will get the tables with ColumnA and ColumnB instead of ColumnA or ColumnB like the accepted answer
SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%city_id%' AND TABLE_SCHEMA='database'
The problem with information_schema is that it can be terribly slow. It is faster to use the SHOW commands.
After you select the database you first send the query SHOW TABLES. And then you do SHOW COLUMNS for each of the tables.
In PHP that would look something like
$res = mysqli_query("SHOW TABLES");
while($row = mysqli_fetch_array($res))
{ $rs2 = mysqli_query("SHOW COLUMNS FROM ".$row[0]);
while($rw2 = mysqli_fetch_array($rs2))
{ if($rw2[0] == $target)
....
}
}