I have this query. When I execute I got Query statement but I want result set of this query.
My query is:
SELECT CONCAT('select * from ', table_name, ' union all ') AS CUSTOMQUERY
FROM information_schema.tables
WHERE table_name LIKE '%custom%'
I get this result
select * from custom_70006 union all
select * from custom_704306 union all
select * from custom_700436 union all
select * from custom_7000643 union all
select * from custom_7000634 union all
select * from custom_700046 union all
select * from custom_700063 union all
select * from custom_700062 union all
But I want result set of this particular column with corresponding data and last union all should remove.
Please help me with relevant query.
I think this is what you are looking for:
SET GLOBAL group_concat_max_len = 4294967295;
SELECT #query1 := GROUP_CONCAT(CONCAT('SELECT * FROM ', table_name) SEPARATOR
' UNION ALL ') AS CUSTOMQUERY
FROM INFORMATION_SCHEMA.tables
WHERE TABLE_SCHEMA = SCHEMA()
AND table_name LIKE '%custom%';
PREPARE stmt FROM #query1; EXECUTE stmt; DEALLOCATE PREPARE stmt;
Example: SQLFiddle
or modify your query as:
SELECT #query1 := CONCAT('select * from ', table_name, ' union all ') AS
CUSTOMQUERY
FROM information_schema.tables
WHERE table_name LIKE '%custom%'
to remove last 'union all' string from your query:
SET #query1 = TRIM(TRAILING 'union all' FROM TRIM(#query1));
PS: Default value of group_concat_max_len is only 1024. So you need to set it to higher value otherwise the output of your query will get stripped and you may get syntax error.
Related
I am trying to write up something that can be run once and exported with all the data from a prepared statement and a select statement. Is it possible to union them?
example dynamic pivot table
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'format(sum(case when field_key = ''',
field_key,
''' then field_value end), 0) ',
field_key
)
) INTO #sql
FROM
Meeting;
SET #sql = CONCAT('SELECT Meeting_id, ', #sql, '
FROM Meeting
GROUP BY Meeting_id'
);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
example select statement
set #startdate = '2020-11-10'
set #enddate = '2020-12-30'
select column1,
column2,
column3,
if(col.id = id,1,0) column4,
if(col.id = id,0,1) column5
from Meeting
group by col.id
order by column2;
If its not possible to UNION ALL them then what would be your suggestions?
Unions have to have the same number of columns (and their names are taken from the first query in the union), and here the pivot query has a dynamic number. You would need to adjust the columns of your second select to match and add it to your #sql as a union.
Often with unions you add a type field that is hardcoded and different for each of the unioned queries, and have each query return null for columns that aren't relevant to that query.
I'am trying to get all tables from a database where table name contains 'logs' and get the sum last value in each table of a column named flag.
Query I tried:
Select SUM(flag) FROM (SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'db_test' AND table_name like '%logs') as c ORDER BY id DESC Limit 1;
But I'am having an issue with the subquery, I think the whole query is wrong.
I have broken this down into baby steps - nothing to stop you adjusting to taste.
drop table if exists onelog,twolog;
create table onelog (id int,flag int);
create table twolog (id int,flag int);
insert into onelog values (1,10),(2,1);
insert into twolog values (1,20),(2,1);
set #sql =
(
select group_concat(
concat('select id,flag from '
,tname, ' where id = (select max(id) from ', tname, ') union all'
)
)
from
(
select table_name tname from information_schema.tables where table_name like '%log' and table_schema = 'sandbox'
) s
)
;
set #sql = substring(#sql,1, length(#sql) - 10);
set #sql = replace(#sql,'union all,','union all ');
set #sql = concat('select sum(flag) from (', #sql , ' ) s');
#select #sql;
prepare sqlstmt from #sql;
execute sqlstmt;
deallocate prepare sqlstmt;
+-----------+
| sum(flag) |
+-----------+
| 2 |
+-----------+
1 row in set (0.001 sec)
I have a multi-schema structure (same table across all schemas).
Here is how I can fetch all the schemas:
select table_schema from information_schema.tables where table_name = 'messages' and TABLE_TYPE = 'BASE TABLE'
This query is for a join between 'messages' table across two tables-
select *, '1' as customer from customer_1.messages union select *, '2' as customer from customer_2.messages
How do I use the perform the above query for all the schemas from the first query? Need not be a join. Could be anything that helps me return the results across all schemas.
Note: There could be 5000-10000 schemas
DELIMITER ##;
CREATE PROCEDURE join_all_data()
BEGIN
SET #counter := 0;
SELECT GROUP_CONCAT('select *, ',
#counter := #counter + 1,
' as customer from ',
table_schema,
'.messages'
SEPARATOR ' UNION ALL ')
FROM information_schema.tables
WHERE table_name = 'messages'
AND table_type = 'BASE TABLE'
INTO #sql;
PREPARE stmt FROM #sql;
EXECUTE stmt;
DROP PREPARE stmt;
END
##;
DELIMITER ;
CALL join_all_data;
I have a table with 46 columns in my database, In these 46 columns 25 or 26 are mostly empty.
when I select all columns by some condition I want only those column will select which have some data in it
You can try this query to show you the not null columns' names:
SET group_concat_max_len = 4294967295;
SELECT GROUP_CONCAT(CONCAT(
' SELECT ',QUOTE(COLUMN_NAME),
' FROM table_name',
' WHERE `',REPLACE(COLUMN_NAME, '`', '``'),'` IS NOT NULL',
' HAVING COUNT(*)'
) SEPARATOR ' UNION ALL ')
INTO #sql
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'table_name';
PREPARE stmt FROM #sql;
EXECUTE stmt;
SQL Fiddle
And then just excute these columns manualy:
SELECT column_name3, column_name8 FROM Table_Name;
SQL Fiddle
I have a bunch of columns in a mysql table whose names begin with 'X_' (for example: X_N, X_Pad, X_Server etc). Now, these columns can be null.
I want to find out which column beginning with 'X_' is NOT NULL the most.
COUNT(expr)
Returns a count of the number of non-NULL values of expr in the rows
retrieved by a SELECT statement.
The following query will return not-null counts for each column:
SELECT
COUNT(*) AS Total,
COUNT(X_N) AS NNC_N,
COUNT(X_Pad) AS NNC_Pad
FROM table;
You can use this query to get the list of matching columns from a table:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '<database_name>'
AND TABLE_NAME = '<table_name>'
AND COLUMN_NAME LIKE 'x\_%';
-- Output:
-- X_N
-- X_Pad
-- X_Server
You can use this query to build a query:
SELECT CONCAT('SELECT ', GROUP_CONCAT('COUNT(`', COLUMN_NAME, '`) AS `NNC of ', COLUMN_NAME, '`'), ' FROM `', TABLE_NAME, '`')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '<database_name>'
AND TABLE_NAME = '<table_name>'
AND COLUMN_NAME LIKE 'x\_%';
-- Output (added whitespace for readability):
-- SELECT
-- COUNT(`X_N`) AS `NNC of X_N`,
-- COUNT(`X_Pad`) AS `NNC of X_Pad`,
-- COUNT(`X_Server`) AS `NNC of X_Server`
-- FROM `<table_name>`
Alternate:
SELECT GROUP_CONCAT('SELECT \'', COLUMN_NAME, '\' AS `Col`, COUNT(`', COLUMN_NAME , '`) AS `NNC` FROM `', TABLE_NAME , '`' SEPARATOR ' UNION ALL ')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '<database_name>'
AND TABLE_NAME = '<table_name>'
AND COLUMN_NAME LIKE 'x\_%';
-- Output (added whitespace for readability):
-- SELECT 'X_N' AS `Col`, COUNT(`X_N`) AS `NNC` FROM `<table_name>` UNION ALL
-- SELECT 'X_Pad' AS `Col`, COUNT(`X_Pad`) AS `NNC` FROM `<table_name>` UNION ALL
-- SELECT 'X_Server' AS `Col`, COUNT(`X_Server`) AS `NNC` FROM `<table_name>`
select count(X_N), count(X_Pad), count(X_Server) from db.tbl;
can be ok.
If you just need a method to generate this SQL, you can use
SELECT CONCAT("select ", GROUP_CONCAT("count(",column_name, ")"), " from ", table_schema, '.', table_name)
FROM information_schema.columns
WHERE table_schema='db' AND table_name='tbl' AND column_name LIKE 'X\_%';
Something like this perhaps? (untested)
SELECT
(SELECT COUNT(id) FROM My_Table WHERE X_1 != NULL) as x1,
(SELECT COUNT(id) FROM My_Table WHERE X_2 != NULL) as x2,
(SELECT COUNT(id) FROM My_Table WHERE X_3 != NULL) as x3,
(SELECT COUNT(id) FROM My_Table WHERE X_4 != NULL) as x4;