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.
Related
I'm searching a way to show only the filled tables' list on a MySQL database schema, in other words by filtering out all the empty tables.
I know that I can perform something like that for show all the tables within a database schema:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'myd_db_schema_name'
What if I want to add an additional WHERE condition for show only NOT NULL tables's list?
I don't know if it helps but you can use two WHERE Clause with AND
And to filter out the empty Tables you can check if the Table has a row like this:
WHERE table_rows >= 1
So the full Query would be
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'myd_db_schema_name' AND table_rows >= 1
I haven't tried it out but hopefully it works.
Okay, this is the solution:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'my_db_schema_name' AND table_rows >= 1
Instead of using COLUMNS I used TABLES.
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 :)
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')
If SQL query contains all fields what I need it will be very large.
How in SQL query I can select field from F3 to F 100 and from F150 to F200?
SELECT F3 to F100, F150 to F200 FROM database;
It is possible or not???
Tables structure change is not available
You have to :
1- manually select all columns . Or
2- do
Select * from database
And then just fetch the columns you need.
There are no shortcuts for this, you will have to list the fields needed one way or another. If the fields being selected are always the same, you should create a view for that as:
CREATE VIEW SomeView AS
SELECT
F3,
...
F100
FROM
SomeTable
and then select like:
SELECT * FROM SomeView
But again, you will have to list the fields at least once.
SELECT F3 to F100, F150 to F200 FROM database;
this query can not possible..
you must have to specify all the columns name
like select F1,f2,f3 from database;
You can't.
But if it's not possible to modify your table structure to fix the database design issue, you can use an SQL query to generate the MySQL query:
SELECT CONCAT('SELECT ', GROUP_CONCAT(COLUMN_NAME), ' FROM `your table`')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'your schema' and TABLE_NAME = 'your table'
GROUP BY TABLE_NAME
Add a filter in WHERE to select only the desired fields.
I have written this simple query which would pull out all the data from table into a CSV file.
SELECT Group_concat(Concat(column_name))
FROM information_schema.columns
WHERE table_name = 'subject_assignment'
AND table_schema = 'newschema2'
UNION ALL
SELECT (SELECT Group_concat('`', column_name, '`')
FROM information_schema.columns
WHERE table_name = 'subject_assignment'
AND table_schema = 'newschema2')
FROM subject_assignment
INTO OUTFILE 'D:\\export\\asd.csv'
Now, the first bit works great but I have issues with the second part.
Instead of pulling out data from columns specified in column list it just displays me all the column names over and over again.
Could you please suggest what I am doing wrong?
Thanks.
In your second SELECT you do not select any column from subject_assignment. Instead, you're selecting single string value made from concatenated column names. And you're selecting it as many times as the row count of subject_assignment.
UPDATE:
If you want to dynamically create column names and then select data from them, see this: https://stackoverflow.com/a/17573774/925196