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;
Related
I have a WordPress multisite database, which has a lot of orphan tables I need to get rid of. The names are structured like this. The number in the table name is the site ID.
wp_9892_wc_booking_relationships
wp_10001_wc_booking_relationships
wp_18992_wc_deposits_payment_plans
wp_20003_followup_coupons
wp_245633_followup_coupon_logs
I want to make a query to find out the highest site ID from the table names.
I've tried queries like this
SELECT table_name FROM information_schema.tables
WHERE table_type = 'base table'
AND table_name REGEXP '^wp_[0-9]+_[a-z0-9]+'
ORDER BY table_name DESC
LIMIT 1;
But that sorts the results in an unexpected way: I get
wp_9_woocommerce_log
When with LIMIT 10 I see there are names with higher numbers:
wp_99_woocommerce_log
wp_999_woocommerce_log
wp_999_wc_webhooks
wp_999_wc_download_log
wp_999_wcpv_per_product_shipping_rules
wp_999_wcpv_commissions
wp_9999_wcpv_per_product_shipping_rules
wp_9999_wcpv_commissions
wp_9998_wc_points_rewards_user_points_log
Is this something that's doable with a SQL query?
Consider the following data example.
CREATE TABLE test(
table_name varchar(255) );
insert into test values
('wp_99_woocommerce_log'),
('wp_999_woocommerce_log'),
('wp_999_wc_webhooks'),
('wp_999_wc_download_log'),
('wp_999_wcpv_per_product_shipping_rules'),
('wp_999_wcpv_commissions'),
('wp_9999_wcpv_per_product_shipping_rules'),
('wp_9999_wcpv_commissions'),
('wp_9998_wc_points_rewards_user_points_log');
Using,
SELECT table_name
FROM test
order by (substring_index(substring_index(table_name, 'wp_', -1), '_', 1) * 1 ) desc ;
Will give the following result:
table_name
wp_9999_wcpv_per_product_shipping_rules
wp_9999_wcpv_commissions
wp_9998_wc_points_rewards_user_points_log
wp_999_woocommerce_log
wp_999_wc_webhooks
wp_999_wc_download_log
wp_999_wcpv_per_product_shipping_rules
wp_999_wcpv_commissions
wp_99_woocommerce_log
https://dbfiddle.uk/590L44Xr
Using substring_index twice we get the number between wp_ and the second _.
* 1 is a shortcut to cast the varchar to int.
In your case it will be something like
SELECT table_name
FROM information_schema.tables
WHERE table_type = 'base table'
AND table_name REGEXP '^wp_[0-9]+_[a-z0-9]+'
ORDER BY (substring_index(substring_index(table_name, 'wp_', -1), '_', 1) * 1 ) DESC
LIMIT 1;
One way is to extryct the numbers and sort it
Example
CREATE TABLE table1
(`tb` varchar(34))
;
INSERT INTO table1
(`tb`)
VALUES
('wp_9892_wc_booking_relationships'),
('wp_10001_wc_booking_relationships'),
('wp_18992_wc_deposits_payment_plans'),
('wp_20003_followup_coupons'),
('wp_245633_followup_coupon_logs')
;
Records: 5 Duplicates: 0 Warnings: 0
SELECT tb FROM table1 ORDER BY REGEXP_SUBSTR(tb,"[0-9]+") + 0 DESC LIMIT 1
tb
wp_245633_followup_coupon_logs
fiddle
So your query will look like
SELECT table_name FROM information_schema.tables
WHERE table_type = 'base table'
AND table_name REGEXP '^wp_[0-9]+_[a-z0-9]+'
ORDER BY REGEXP_SUBSTR(table_name,"[0-9]+") + 0 DESC
LIMIT 1;
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
I'm astounded by the number of tables in Magento Enterprise 1.13 (over 200). I'm trying to get a handle on the way things are organized and I think it would be helpful to know the number of columns in each of the tables. The following query will get me a breakdown of columns and their data_types for each table:
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = `<database_name>`
ORDER BY TABLE_NAME;
But I would also like to know the number of columns in each table.
SELECT COUNT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '<database_name>'
AND TABLE_NAME IN (
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '<database_name>'
);
Unfortunately, the above query returns a count of the total number of columns in the database. I realize that my approach is too simplistic and a LOOP or a FOREACH statement is closer to the solution I'm looking for but I don't know how to make the leap to that point.
SELECT TABLE_NAME, COUNT(COLUMN_NAME) AS NoCOLUMNS
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '<database_name>'
AND TABLE_NAME IN (SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '<database_name>')
GROUP BY TABLE_NAME;
Basic GROUP BY DEMO
Is it possible to get the custom result when executing below query:
show table status from dbname
I customized "show processlist" query in this way:
SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST where time > 4 order by TIME desc;
In the same way, I want to get the custom result from above query.
Many thanks for your suggestions...
After going through information_schema, I got my answer which I want to share here.
SELECT * FROM information_schema.tables WHERE table_schema = 'dbname';
If want to list down only some specific columns, we can mention the column names separated by comma just after SELECT key. Also, we can add filter records by adding conditions in WHERE clause. For example:
SELECT table_name,table_type,Engine,version,table_rows FROM information_schema.tables WHERE table_schema = 'jprod';
There are only two differences between below queries:
(a)show table status from dbname;
(b)SELECT * FROM information_schema.tables WHERE table_schema = 'dbname';
Query (b) provides 4 extra columns - (i) Table_catalog (ii) Table_schema (iii) Table_type (iv)Checksum
Some column names in query (a) is brief likewise table_name as name, Table_rows as rows, table_comment as comment.
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;