Hi, when entering the following url (for learning purpose only) I can see an image with id=1.
So I tried this to find the names of some tables inside the database, and it worked. This can detect if a table starting with 'albu' exists or not:
http://35.227.24.107:5001/d0510b3060/fetch?id=1 AND (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'albu%')>0; --
But how could I know if a specific column that its name starts with 'albu' exists or not? I already tried this, but it didn't work:
http://35.227.24.107:5001/d0510b3060/fetch?id=1 AND (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'albums' AND column_name LIKE 'albu%')>0; --
Please Note: for my question, I know both the name of the db and the table.
As commented by Shadow, you are looking for MySQL INFORMATION_SCHEMA.COLUMNS table :
SELECT COUNT(*)
FROM information_schema.columns
WHERE table_name = 'albums' AND column_name LIKE 'albu%'
NB : you might also want to filter on the TABLE_SCHEMA. It takes the schema and the table name to uniquely identify a table in the database.
Related
I am trying to make a select query where i select all the columns from a table and add the table name as another column.but i don't know how to proceed with this.
SELECT t.name , t.table_name FROM `glassfilms` as t WHERE `name` LIKE '%007%'
here t.table_name is not a column. I need the columns name and table_name
From my concern, it is not a good idea to get a table name with the select statement because you have to add your database name and tables name which has confidential data for your project.
By the way, I solved your problem with the below query.
SELECT t.name,
(select table_name from information_schema.tables where TABLE_SCHEMA = '{{your_database_name}}' and table_name = 'glassfilms') as tbl_name
FROM glassfilms t
WHERE t.name like "%007%";
i found some answers but i found some answers here: Get the list of tables created on any date? but im getting, sys.tables doesn't exists.
i know here sys is database name.
If you want to find by a particular created date then:
SELECT * FROM information_schema.tables where TABLE_SCHEMA = 'YOUR_DATABASE_NAME'
AND CREATE_TIME = '2019-12-23 17:41:05'
Look into tables table in information_schema db. Query below:
SELECT table_schema,table_name,create_time
FROM information_schema.tables
WHERE table_schema=*your_database_name
AND table_name=*your_table_name;
Replace *your_database_name and *your_table_name accordingly.
Edit: sorry, your question is to lookup by created date.. here's the query:
SELECT table_schema,table_name,create_time
FROM information_schema.tables
WHERE DATE(create_time)="yyyy-mm-dd"
You can try it here : https://www.db-fiddle.com/f/n1yPjJKhyMLkWSP25htovn/0
Similar to How to find all the tables in MySQL with specific column names in them? I would like to find the table with 2 specific columns, not either or.
I've tried combining with AND but no dice.
For instance, I want to search the database for the specific tables that contain both CategoryID and LotNumber columns.
Through the information_schema.columns table, grouping the matching columns by table and returning only those with number equal to 2:
SELECT table_name
FROM information_schema.columns
WHERE (column_name = 'colname1' OR column_name = 'colname2')
[AND table_schema = 'dbname']
GROUP BY table_name
HAVING count(*) = 2;
You can try something like :
SELECT * from TableName where obj1 = "obj1" and obj2= "obj2"
this is an example.
Let me know how it worked :)
What would be the fastest way to delete all records in every table of the database with id = 0?
It would be simple if every table had its first column called id, but in my case one table has first column named id_tag, other table - id_product, etc.
I've figured out that I can get the name of the first column by:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'dbname' AND TABLE_NAME = 'ps_tag' LIMIT 1
but how can I include it in a DELETE query? I need something like:
DELETE FROM 'ps_tag' WHERE [first_column] = 0
My first idea was:
DELETE FROM 'ps_tag'
WHERE (SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'dbname' AND TABLE_NAME = 'ps_tag' LIMIT 1) = 0
but obviously it doesn't work.
Although you can set up a dynamic SQL statement, I often find this type of operation is easier to do in Excel. Write a query to get the column name and table name for the ones you are interested in.
Then, load these into Excel.
In another cell, put in a string like 'delete from #table where #column = 1'.
Then put in the formula:
=substitute(substitute(<where the string is>, '#table', <tablename>), '#column', <columnname>))
Copy the code back to your database interface and execute it.
(And any spreadsheet will do. I usually have Excel handy.)
I am trying to write a query which lists the names of the columns in an SQL table, however, I don't want all the columns - just specific ones. So, for example, if I was to put the COMMENT = 'test' for the columns which I want to list then I thought my query would be:
SHOW COLUMNS FROM `tbl_name` WHERE `COMMENT`='test'
This however throws an error.
Any ideas?
Thanks,
I think you can do this using information_schema.columns:
select column_name
from information_schema.columns c
where table_name = 'tbl_name' and
column_comment = 'test';
I think that SHOW COLUMNS can't have the WHERE clause, but you can try this:
SHOW COLUMNS FROM (SELECT * FROM `tbl_name` WHERE `COMMENT`='test')