MYSQL - List specific columns - mysql

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

Related

SQL query - I don't know all identifiers

Is there a way, where I can see every parameter or identifier I can query from my database? Not the contents but the "column names"
Something like
SELECT * FROM myDb AS String
To simply get the column names and types of a table.
You could SHOW them.
SHOW COLUMNS FROM myTable;
But if you want to know the column names of your table, and only a bit of data from it (to see what it looks like).
Then use LIMIT to get only a few records.
SELECT *
FROM myTable
LIMIT 3
It's fast and easy.
But you can also just see the columns without data if you use a criteria that's false.
SELECT *
FROM myTable
WHERE 0=1
You can also use:
show create table table_name;
but as "LukStorms" mentioned, the below statement shows you the data in table format and in a pretier way
show columns from table_name;
You can use INFORMATION_SCHEMA.COLUMNS to retrieve all columns name
select column_name from INFORMATION_SCHEMA.COLUMNS where Table_Name='Your_Table'

How do I find a table in MySQL with 2 specific columns in it

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 :)

how to select dynamic columns which are result of another query in mySQl

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.

MySQL INTO OUTFILE Query problems

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

MySql show tables query

I'm trying to select the tables names in my database using this query
show tables LIKE 'table1%'
it's working fine but at the same table I have a version of the table with some columns empty called 'table1_blank'. When I use the above statement I get every table starting with table1 including the "blank", how do I exclude 'table1_blank' from the selection?
Thanks.
You can use multiple like clauses using the where condition for SHOW TABLES:
SHOW TABLES
FROM `dbname`
WHERE `Tables_In_dbname` LIKE 'table1%'
AND `Tables_In_dbname` NOT LIKE 'table1\_%';
'_' has special meaning. 'any one character', so you need to change like this..
EDITED
SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'your database name'
AND TABLE_NAME LIKE 'table1%'
AND TABLE_NAME NOT LIKE 'table1\_%';