Let's say i have 'db_institute' and 'tb_data' which have 39 Column
While This Answers Selecting from the last 10 column, i want to add extra column in the query
the result would be like this when i prepared statement it
SELECT Name,lastcolumn1,...,lastcolumn10 from tb_data
And here's my failed attempt
SELECT Name,
CONCAT('SELECT ',
GROUP_CONCAT(COLUMN_NAME),
' FROM 'tb_data')
FROM
(SELECT
COLUMN_NAME,
ORDINAL_POSITION
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_SCHEMA='db_institute'
AND
TABLE_NAME='tb_data'
ORDER BY
ORDINAL_POSITION DESC LIMIT 10) AS ord_desc
ORDER BY
ord_desc.ORDINAL_POSITION
into #sql
Which result error
Unknown column 'Name' in 'field list'
your reference is already correct. try this
SELECT
CONCAT('SELECT Franchisee, ', GROUP_CONCAT(COLUMN_NAME), ' FROM product_staging') # change product_staging to your table name and Franchisee to Name
FROM
(SELECT
COLUMN_NAME,
ORDINAL_POSITION
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_SCHEMA='myob' # change it to your schema name
AND
TABLE_NAME='product_staging' # change it to your table name
ORDER BY
ORDINAL_POSITION DESC LIMIT 10) AS ord_desc
ORDER BY
ord_desc.ORDINAL_POSITION
INTO #qry;
PREPARE stmt1 FROM #qry;
EXECUTE stmt1;
Related
I'm looking for a schema-independent query. The query should be equally capable of catching duplicate rows in either table in a database.I have number of tables without primary key. I have found a result for sql server [which i have most experience] but looking for same thing in mysql
Use dynamic SQL to generate the query using the column information in information_schema:
SET #tablename = 'yourTable';
SET #sql = (
SELECT CONCAT('SELECT *
FROM `', table_name, '`
GROUP BY ', GROUP_CONCAT(CONCAT('`', column_name, '`')), '
HAVING COUNT(*) > 1')
FROM information_schema.columns
WHERE table_name = #tablename );
PREPARE stmt FROM #sql;
EXECUTE stmt;
I want to copy/update data from Table A to Table B. Table B has some more additional columns. I have tried the following options.
1) `REPLACE INTO `B` (SHOW FIELDS FROM 'A') SELECT * FROM `A
2) `REPLACE INTO `B`
(SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='test1' AND `TABLE_NAME`='A') SELECT * FROM `A
But it throws errors. Can you guys help me how to select names with select query?
UPDATE:
3) As suggested by Jerko,
I have two tables A(warehouse_id,long,lat) B(warehouse_id,long)
Applied the following statement.
SET #query = CONCAT('REPLACE INTO `A` (SELECT ',
(SELECT GROUP_CONCAT(CONCAT('`',column_name, '`'))
FROM information_schema.columns
WHERE `TABLE_SCHEMA`='test2' AND `table_name` = 'A'),
' FROM `B`)');
PREPARE stmt FROM #query;
EXECUTE stmt;
This gives me the error
"#1054 - Unknown column 'lat' in 'field list' "
You can't do this dynamically in mysql like you are trying to do. MySQL expects your list of column names to be provided directly, not from a subquery.
If you want to do this dynamically you'll have to step back upstream to whatever language you are using to interact with MySQL such as PHP or Java.
Have you tried this?
insert into B(col1, . . ., coln)
select col1, . . ., coln
from A;
That is, list the fields from A in the select clause. List the corresponding columns for B in the insert column list.
If you need the list of columns, get them from INFORMATION_SCHEMA.COLUMNS and cut-and-paste into the query.
Actually there is a way
SET #query = CONCAT('REPLACE INTO `A` (',
(SELECT GROUP_CONCAT(CONCAT('`',column_name, '`'))
FROM information_schema.columns
WHERE `TABLE_SCHEMA`='test1' AND `table_name` = 'A'
AND column_name IN (SELECT column_name FROM information_schema.columns WHERE table_schema = 'test1' AND table_name='B')) ,
') (SELECT ',
(SELECT GROUP_CONCAT(CONCAT('`',column_name, '`'))
FROM information_schema.columns
WHERE `TABLE_SCHEMA`='test1' AND `table_name` = 'A'
AND column_name IN (SELECT column_name FROM information_schema.columns WHERE table_schema = 'test1' AND table_name='B')),
' FROM `B`)');
PREPARE stmt FROM #query;
EXECUTE stmt;
Try this
INSERT INTO B (field1,field2,...,fieldN)
SELECT (field1,field2,...,fieldN) FROM A
I would like to get the values of the auto_increment column in my table (example). The catch is however, that I don't have the name of the auto_increment field. I'm currently using the following query to determine the name of the field:
SELECT column_name FROM information_schema.columns WHERE table_name = 'example' AND extra = 'auto_increment' LIMIT 1;
I would now like to pass the result of this query, as a 'string' to my actual query, and get the value. If I would like to do this in one go, how would I do that, because the below query, which should give me all auto_increment values used, only yields the above result -namely the auto_increment column name.
SELECT (
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'example'
AND extra = 'auto_increment'
LIMIT 1
) AS pri
FROM example
Any thoughts would be appreciated :)
Many Regards,
Andreas
Here is an example of how you would do this using prepare and execute:
SELECT #s := concat('select `', column_name, '` from example e')
FROM information_schema.columns
WHERE table_name = 'example' AND extra = 'auto_increment'
LIMIT 1
prepare stmt from #s;
execute stmt;
deallocate prepare stmt;
I would like to have a list of those columns of a table that have at least one non-NULL data entries in them.
In other words, I would like to get the column names for which the following returns at least one entry:
SELECT DISTINCT column_name FROM table WHERE column_name IS NOT NULL
I tried the following:
SELECT column_name
FROM information_schema.columns
WHERE table_name = "table_name"
AND EXISTS (
SELECT DISTINCT column_name FROM table_name WHERE column_name IS NOT NULL
)
But this also returns the column names where all the entries are NULL.
So how do I get only those columns with non-NULL entries?
Create from the INFORMATION_SCHEMA.COLUMNS table a string that contains the SQL you wish to execute, then prepare a statement from that string and execute it.
The SQL we wish to build will look like:
SELECT 'column_a'
FROM table_name
WHERE `column_a` IS NOT NULL
HAVING COUNT(*)
UNION ALL
SELECT 'column_b'
FROM table_name
WHERE `column_b` IS NOT NULL
HAVING COUNT(*)
-- etc.
(One could omit the WHERE clause and substitute COUNT(*) for COUNT(column), but I think that might be less efficient on indexed columns).
This can be done using the following:
SET group_concat_max_len = 4294967295;
SELECT GROUP_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;
DEALLOCATE PREPARE stmt;
See it on sqlfiddle.
Use this procedure this will print columns names of a table which have atleast one not null rows.
create or replace procedure list_col_notNull(tblName in varchar2)
as
lv_col_name varchar2(200);
lv_ctr number;
lv_sql varchar2(400);
CURSOR cur_col_name is
SELECT column_name
FROM USER_TAB_COLUMNS U
WHERE table_name = tblName order by column_name asc;
begin
open cur_col_name;
LOOP
FETCH cur_col_name INTO lv_col_name;
EXIT WHEN cur_col_name%NOTFOUND;
lv_sql := 'select count(1) From ' || tblName || ' where ' || lv_col_name || ' is not null' ;
EXECUTE IMMEDIATE lv_sql into lv_ctr;
if lv_ctr > 0
then
dbms_output.put_line(lv_col_name);
end if;
I have 10 fields in my table but i need 8 fields when i select , i dont want to specify select 1,2,3,4,5,6,7,8 from........ ,
Any easy way to get the 8 fields (Other hand i dont want to select primary,unique key fields)
see the answer in this :
Select all columns except one in MySQL?
Actually there is a way, you need to have permissions of course for doing this ...
SET #sql = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), '<columns_to_delete>,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '<table>' AND TABLE_SCHEMA = '<database>'), ' FROM <table>');
PREPARE stmt1 FROM #sql;
EXECUTE stmt1;
Replacing <table>, <database> and <columns_to_delete>