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
Related
Hi everyone I have multiples tables which I want to get tables with specific column name which I am able with the following code:
SELECT COLUMN_NAME AS 'ColumnName'
,TABLE_NAME AS 'TableName'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'DATABASENAME'
AND COLUMN_NAME LIKE '%columnName_I_Need%' // Example not actual search
ORDER BY TableName
,ColumnName;
Now I want to get the all data from the resulted tables.
For example, get all columns and their data in resulted tables.
This is an example but is not working :
SELECT * WHERE columnName_I_Need = 1
Is this possible with MySQL?
MySQL version: 5.5.5-10.3.23 MariaDB
This is quite complicated. If you used only dynamic SQL, you would need a looping mechanism. My recommendation is to generate the SQL and then run it manually:
SELECT GROUP_CONCAT('SELECT * FROM ', TABLE_NAME,
' WHERE ', COLUMN_NAME, ' = 1'
SEPARATOR ';
'
)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'DATABASENAME' AND
COLUMN_NAME LIKE '%columnName_I_Need%';
Then copy the statement and run it manually.
The 'FROM' is missing in your query. Also when you specify a column name it should be written inside single quotes.
SELECT * FROM your_table_name WHERE columnName_you_Need = 'colum_id'
This is a simple and basic query to access the data from the table.
SELECT * FROM tablename
WHERE can include a condition here.
* is used to select each and every column.
One can use the column name to access a particular column.
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')
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 was looking for this Stackoverflow Question to find my solution. But this one deals with when the column name is known. But in my case the column name is unknown. i.e. it can be col1, col2, or colN.
Right now I only have the table names from my database using this query:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='db_name';
Now with another query(joining together) I want to scan the tables for all the columns that match the given value. Is it possible with MySql using any built-in function? Or Can any sql tweaking do this trick?
If its one time activity, why cant you run a query to generate the query that give you result.
I dont know a better solution :)
Queries will be something like:
SET group_concat_max_len = 100000;
SELECT GROUP_CONCAT(
CONCAT( 'select "',TABLE_NAME,'.',COLUMN_NAME, '" from `',TABLE_NAME,'` where `', COLUMN_NAME, '` in ("val0", "val1") limit 1')
SEPARATOR ' UNION ALL ' ) as query
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='db_name' INTO OUTFILE '/tmp/queries.sql' \G
Now you can run source the file.
SOURCE /tmp/queries.sql;