How do i find similar column names from a database table?
for e.g.
a database table
1_1 1_2 1_3 5_6 67
| | | |
| | | |
So, 1_1, 1_2, 1_3, 5_6 and 67 are the column names of a database table. And i would like to retrieve only the column names starts with 1 (1_1, 1_2 and 1_3). i tried the sql query but it dint work..
SELECT 1 LIKE '%1%' FROM sheet1;
It shows something of this short
1 LIKE '%1%'
1
1
Documentation find here
SHOW COLUMNS FROM tbl_name FROM db_name
LIKE '1%'
To get the contents of the respective column:
SQL Fiddle
this may be useful to you
SELECT COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME='table' AND TABLE_SCHEMA='database_name'
information_schema is database containing meta data about all databases so when ever you want this kind of data you simply fire query on it
Try this:
SELECT GROUP_CONCAT(COLUMN_NAME) INTO #s
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'database_name' AND TABLE_NAME = 'tableName' AND
COLUMN_NAME LIKE '1%';
SET #sql = CONCAT('SELECT ', #s, ' FROM tableName');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Related
I have seen a lot of similar questions but not exactly what I'm looking for. I need to convert a single column of
I'm using
SELECT Column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE Table_name = 'LibraryInventory'
to get the column names of my table. However, they're being returned in a single row such as this
ID
-----|
NAME
-----|
TITLE
-----|
I need a way to have these put into a long single row of columns so I can use it in Java how I'm planning like: |ID| NAME| TITLE|
Does anyone know a simple way to do this? I've seen people use pivot however they have much more complicated tables so struggled to understand it.
You can use a prepared statement:
SELECT #query :=
group_concat(concat('''', Column_name, ''''))
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE Table_name = 'LibraryInventory' ;
set #query = concat(' SELECT ', #query, ''';');
prepare stmt from #query;
execute stmt;
I have an old script that creates each day a table and store data in it.
The table names are like this today-date-some-other-string, i.e : 02-02-2018-other-string.
The question is, how could I describe the structure of that table despite I only have today's date? I mean is there a way to do something like this :
DESC WHERE Table like "02-02-2018%"
Thank you.
In mysql you could use prepared statements for example
set #sql = concat('describe ' , (select table_name
from information_schema.tables
where table_name like 'users' and table_schema = 'sandbox')
,';');
prepare sqlstmt from #sql;
execute sqlstmt;
deallocate prepare sqlstmt;
I can write a query to search for a table that has a particular column in a DB
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME like '%A'
but My Question is:
can I search an entire DB for a value in a column?
So I'm unsure the name of the column and I am unsure the name of the DB table but I know the value is 'Active'
Yes, you can. In that case, you need to prepare dynamic query once you get list of tables, which consists column, which actually you are looking for.
Now create a cursor for
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME like '%A'
Using above cursor loop below
SET #s = CONCAT("select count(*) from [tablename] where [columnname] like ","'%SOMETHING%'");
PREPARE stmt FROM #s
execute stmt;
DEALLOCATE PREPARE stmt;
I have a few columns that was built in like this:
| FOOD_AUS | FOOD_JAP | FOOD_KOR | FOOD_CAN |
Is there a way to select it dynamically? I mean like for example if the user will only enter the text AUS all the rows under FOOD_AUS will return.
It is doable, but not in a single sql call. What you can do is to query information_schema.columns view for matching column names from a table and then create an sql statement using the results in a string variable and execute it via prepared statement.
I would do sg along the following lines:
select #c:=group_concat(column_name) as col_names
from information_schema.columns
where table_name='yourtable' and column_name like '%aus%'
group by table_name;
set #sql:=concat('select ',#c, ' from yourtable');
prepare stmnt1 from #sql;
execute stmnt1;
deallocate prepare stmnt1;
I have a table of tables called TABLES. TABLES has a column TABLE_NAME.
For each TABLE_NAME there exists a table in the database. All of these tables are structured the same.
Ultimately I would like to UNION columns from each of these tables. But I'm not getting close to that objective.
I am stuck on the following query:
SELECT column_name FROM (SELECT table_name FROM TABLES) T;
The error returned is "ERROR 1054 (42S22): Unknown column 'column_name' in 'filed list'.
I haven't seen a good representation of SELECT...FROM (SELECT...FROM)
Any help is most appreciated.
There is no way to achieve this with a static query. If you really need this for some reason you have to use dynamic SQL
SET #sql = NULL;
SELECT GROUP_CONCAT(CONCAT('SELECT column_name FROM `', table_name, '`') SEPARATOR ' UNION ALL ')
INTO #sql
FROM tables;
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Here is SQLFiddle demo
To simplify things on the calling side you can always wrap it in a stored procedure.
You can try
SELECT column_name FROM (SELECT column_name FROM TABLES) T;
OR
SELECT column_name FROM (SELECT * FROM TABLES) T;