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%";
Related
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.
I have a database with hundreds of tables and I want to search all the tables to find a table which contains the word Job.
So something like
Select all tables where name = 'JOB';
If you have dba access, you can do this
SELECT *
FROM all_tables
WHERE upper(table_name) like '℅JOB℅'
For MYSQL, you can use information_schema.TABLES
Like,
select * from information_schema.TABLES WHERE TABLE_SCHEMA LIKE 'YOUR_DB_NAME' AND TABLE_NAME LIKE '%SEARCHWORD%'
The result of following query is all tables that 'JOB' is partial of name of those
SELECT o.object_Name, OBJECT_TYPE
FROM user_objects o
WHERE o.OBJECT_TYPE = 'TABLE'
AND o.OBJECT_NAME like '%JOB%'
I have two tables.
First table have column called column_name which contains values columns names of the second table like ( column1 , column2 , etc).
I need to select columns from the second table depending on the result of query column_name from the first table .
I need help solving this issue but I cant develop it:
If You work with Oracle, i suggest You to use listagg function (http://www.oracle-developer.net/display.php?id=515)
And Query will be like:
Select column_name from all_tab_columns where table_name = name_of_your_table2 and column_name in (select listagg...)
if I understand correctly.
Suppose I have 4 tables, named:-
tbl_user
tbl_doctor
tbl_chat_request
tbl_payment
Now three tables have a field called users_id
Is there are query which can tell me all the tables which has a field with column name users_id?
I don't even have any idea if it is possible or not.
Get table name from schema like below
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME ='users_id'
AND TABLE_SCHEMA='db_name';
Try This Query
SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME ='Your Column Name' AND TABLE_SCHEMA='Your Database Name'
So, we managed to do interesting things to our database that created invalid views. We just want to drop these views from the database and move on.
What I could not find is an easy way to find all invalid views in the database so that I can work from there. Is there an easy way to do this?
Recipe to create an invalid view
create table some_table (some_column varchar(20));
insert into some_table(some_column) values('some_data');
create view some_view as (select some_column from some_table);
select * from some_view;
# Now drop the table and test the view
drop table some_table;
select * from some_view;
The solution from Ralph works fine. If you want a query without subselect, try this:
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_type = 'view'
AND table_rows is null
AND table_comment like '%invalid%'
The condition table_rows is null is important as it will force an evaluation of the view and the error message in the table_comment column.
If afterwards you want to fix your view, you can see the original definition with
SELECT view_definition
FROM information_schema.views
WHERE table_schema = 'your_database'
AND table_name = 'your_view'
A better way of finding broken views within your MySQL database:
SELECT vws.table_schema,vws.table_name
FROM (
SELECT *
FROM information_schema.tables
WHERE table_type='VIEW'
AND table_comment LIKE '%invalid%'
) vws;
Original source of query
SELECT TABLE_NAME
FROM information_schema.VIEWS
WHERE TABLE_NAME NOT IN (
SELECT TABLE_NAME
FROM information_schema.TABLES
)
Based on an answer that was somehow deleted.
SELECT CONCAT('CHECK TABLE ', table_name, ';') AS my_view_check_statements
FROM information_schema.views
WHERE table_schema = 'your_database_name'
INTO OUTFILE '/tmp/chkstmts.sql';
source '/tmp/chkstmts.sql';