how to find table with particular pattern in the name - mysql

In my database, I have a set of tables with names such as table<N> where N is a non-negative integer. I need to find the table with the 'greatest' name in the lexicographical order. That is the name with the greatest N. SHOW TABLES lists all the tables, and I could not find a way to do it more efficiently than fetching the whole list in to the client and scanning it. Any suggestions?

Could you use the INFORMATION_SCHEMA.TABLES to achieve this?
Something like:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
ORDER BY table_name DESC
LIMIT 1
You can, of course, add a WHERE clause to this too:
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE table_name LIKE ('numberedTable%')
ORDER BY table_name DESC
LIMIT 1
if applicable.

Related

How can I make a random query what is sorted in mysql?

I'm trying to have a mysql query that retrieve ramdon rows but in an specific order.
For exmple, I have a table with 10000 names, and want to take 10 random names in alfabetical order.
If I use order by rand() and limit together, I can have ramodon rows, but not in order. And I've also see that it's not possible to use order by rand with antoher orders by.
Right now, I'm retrieving ramdom rows and sorting the array after, but I'd like to do this in my query. It is possible? Where can I find good info about this? I was reading at mysql.com but didn't find the solution. Thanks a lot (and sorry about my poor english).
You could use an additional nested query for the sorting:
SELECT name
FROM (SELECT name
FROM mytable
ORDER BY RAND()
LIMIT 10) t
ORDER BY name ASC;
Use this:
SELECT *
FROM (
SELECT *
FROM yourTable
ORDER BY RAND()
LIMIT 10) DT
ORDER BY
Name
like this
select randed.name
from (select name
from user
order by rand()
limit 3
) randed
order by randed.name

Select distinct index names

I need to select all indexes from a given table.
But, it looks like MySQL creates multiple indexes with the same name with all the possible column combinations when there is more than one column in the index.
So SHOW INDEXES returns multiple times the same value.
Is there a way to do a select distinct to get the index names from a given table?
And if possible that is not MySQL specific.
Please try this query:
SELECT DISTINCT INDEX_NAME FROM information_schema.statistics
WHERE table_schema = 'your_schema'
AND table_name = 'your_table'

difference in DataBases

How to make query to information_schema.tables to get list of available tables which is exist in one DB but not exist in another one, something like diff but more suitable. I just need sql query.
So i have Db's like A,B,C,D and all these DB's should has the same tables, how I can check it ?
try
select *
from INFORMATION_SCHEMA.tables
group by table_name
having count(table_schema) < 4
if you have 4 DB's. If more you have to adjust the having clause.
This query give you all unique tables in all databases.
SELECT *,count(TABLE_NAME)
FROM
`TABLES` group by table_name
having count(TABLE_NAME)=1
And if you want repeated table names then use this
SELECT *,count(TABLE_NAME)
FROM
`TABLES` group by table_name
having count(TABLE_NAME)>1

Is there a way to fetch the first entry of a table in database using SQL query?

I have a database table which stores names of people, i want to fetch the first entry of my database table. Is it possible using SQL query?
Use the following:
SELECT * FROM names LIMIT 1
And if you want the first sorted entry, then use the following:
SELECT n.* FROM names n ORDER BY n.last_name ASC, n.first_name ASC LIMIT 1
you can use
select first(column_name) from table_name order by primary_id
first().is an aggregate function available for mssql
you can also go for 'top' in mssql

How to get the total numbers of rows stored in the MySQL database table?

Is there a MySQL command to count the number of rows in a table, and if so, what is it?
MySQL COUNT() function
SELECT COUNT(*) FROM table
A nice way to get AN ESTIMATE of the number of rows can be via meta-data information in the information_schema tables.
Note, this is just an ESTIMATE used for query optimizations.
There may be a way to get exact # of rows from the information_schema, but I am not 100% sure if this is possible.
SELECT table_schema, table_name, table_rows
FROM information_schema.tables
ORDER BY table_rows DESC
This gives you the number of rows from a specific table name that you set:
SELECT TABLE_ROWS from information_schema.TABLES where table_name = "your_specific_table_name_here";