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;
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;
Please explain me the below example
SELECT GROUP_CONCAT(COLUMN_NAME)
FROM information_schema.`COLUMNS` C
WHERE table_name = 'table_name'
AND COLUMN_NAME =('columns_name') INTO #COLUMNS;
SET #table = 'table_name';
SET #s = CONCAT('SELECT ',#columns,' FROM ', #table);
PREPARE stmt FROM #s;
This pattern is all about creating dynamic (prepared in MySQL parlance) queries based on the names of columns in a particular table. INFORMATION_SCHEMA is a built-in database with read-only tables describing all the tables in all databases on the MySQL server.
The first query in your sequence retrieves a text string in the local variable #COLUMNS with a value like
id,name,value,description
for a table named table_name with those four columns.
The third one retrieves a string in the local variable #s with a value containing a query like
SELECT id,name,value,description FROM table_name
The fourth one, PREPARE, gets ready to do EXECUTE stmt, which runs the query. You can read about PREPARE and EXECUTE here.
The whole sequence of queries in your question does almost exactly the same thing as SELECT * FROM table_name.
There's a defect in your first query. You should add AND TABLE_SCHEMA = DATABASE() to its WHERE clause. Otherwise, you may pick up columns from tables named table_name in multiple databases.
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 am trying add one column in my Mysql database that sums all the columns starting by 'tokenvalid' which can take the value of 1 or 0.
And let's say I have 50 columns like that in my database (i.e. tokenvalid1, tokenvalid2 ...., tokenvalide50) with other columns between.
Please find below the code I would like to implement. I know that is not correct at all but it is just to give you an idea of what I am trying to do.
Thank you for your help!
'SELECT *, sum(column_name LIKE "tokenvalid"%) as total FROM points WHERE 1'
This post should help you. The post describes how to get the columns and then query for results.
MySQL Like statement in SELECT column_name
Something like this should help you.
SET #colname = (SELECT GROUP_CONCAT(`column_name`) from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='points' AND `column_name` LIKE 'tokenvalid%');
SET #table = 'points';
SET #query = CONCAT('SELECT SUM(',#colname,') FROM ', #table);
PREPARE stmt FROM #query;
EXECUTE stmt;
Similar to this answer by RocketDonkey
If the string is in your external application (like PHP), sure, just construct the MySQL statement.
If the string is inside a MySQL table, you can't. MySQL has no eval() or such function. The following is impossible:
Suppose you have a table 'queries' with a field "columnname" that refers to one of the column names in the table "mytable". There might be additional columns in 'queries' that allow you to select the columnname you want...
INSERT INTO queries (columname) VALUES ("name")
SELECT (select columnname from queries) from mytable
You can however work with PREPARED STATEMENTS. Be aware this is very hacky.
SELECT columnname from queries into #colname;
SET #table = 'mytable';
SET #s = CONCAT('SELECT ',#colname,' FROM ', #table);
PREPARE stmt FROM #s;
EXECUTE 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;