MySQL query to select table names by column names - mysql

I'm trying to select all tables from a specified database that contain columns 'lang' and 'project'. This is what i'm doing:
SELECT DISTINCT(ISC1.TABLE_NAME) AS `table` FROM INFORMATION_SCHEMA.COLUMNS AS ISC1
JOIN INFORMATION_SCHEMA.COLUMNS AS ISC2 ON
(ISC1.TABLE_SCHEMA=ISC2.TABLE_SCHEMA AND ISC1.TABLE_NAME=ISC2.TABLE_NAME AND ISC2.COLUMN_NAME='project')
WHERE ISC1.COLUMN_NAME='lang' AND ISC2.COLUMN_NAME='project' AND ISC1.TABLE_SCHEMA='some_database'
It does work, but i have a feeling it is a bad way to write this kind of query. If someone can improve on it, that would be nice.
Now i have to change this query to select all tables that have 'lang' column, but do NOT have 'project' column. And honestly i can't figure out where to start...
Thanks for your help

SELECT DISTINCT(ISC1.TABLE_NAME) AS `table`
FROM INFORMATION_SCHEMA.COLUMNS AS ISC1,
INFORMATION_SCHEMA.COLUMNS AS ISC2
WHERE ISC1.TABLE_SCHEMA=ISC2.TABLE_SCHEMA
AND ISC1.TABLE_NAME=ISC2.TABLE_NAME
AND ISC2.COLUMN_NAME='project'
AND ISC1.COLUMN_NAME='lang'
AND ISC1.TABLE_SCHEMA='some_database'
Without project column :
SELECT DISTINCT(ISC.TABLE_NAME) AS `table`
FROM INFORMATION_SCHEMA.COLUMNS AS ISC
WHERE ISC.COLUMN_NAME='lang'
AND ISC.TABLE_SCHEMA='some_database'
AND NOT EXISTS(SELECT *
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE C.COLUMN_NAME = 'project'
AND C.TABLE_NAME=ISC.TABLE_NAME
AND C.TABLE_SCHEMA=ISC.TABLE_SCHEMA)

Related

Get columns names from the two tables

I wanted to get the column_names of the tables when output to csv file when combining two tables with union and below is the one i have tried export. I know there is a question regarding this but that doesnot suit my requirement and hence seeking for help.
Note: Dont close this issue
select GROUP_CONCAT(CONCAT("'",COLUMN_NAME,"'"))
from INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 't1,t2'
AND TABLE_SCHEMA = 'database'
UNION ALL
select * from table t1 LEFT JOIN table t2 on t1.result=t2.result order BY ORDINAL_POSITION limit 5 INTO OUTFILE '/tmp/result.csv';
I am not sure if this is the rightway but i have big column lists.

select Query from a table and plus table name as a column

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%";

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%';

How to find all invalid views in mysql?

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';

MySQL, search a database and tell what tables contain two specific columns?

I have a database with well over 90 tables and I am trying to figure which, if any, of them have the same two specific columns. The code I am looking for would be something like this:
SHOW TABLES IN `database`
WHERE column = 'columnA'
AND column = 'columnB';`
Is this even possible?
This will give you all tables having either of the two columns, which you can browse through to find what you need.
select *
from information_schema.columns
where column_name in ('columnA', 'columnB')
order by table_name, column_Name
SELECT TABLE_SCHEMA AS `schema`, TABLE_NAME AS `table`, COLUMN_NAME AS `column`
FROM `information_schema`.`COLUMNS`
WHERE COLUMN_NAME IN('column1','column2')