My MySQL database has column names like clnt_1001,clnt_1002 .... so how can I fetch the value from all clients runtime? ,
where clients can be added later,
like where column name like '%CLNT_%' and id =2001;(all values in 'CLNT_' are integers)
You will need to query INFORMATION_SCHEMA to get the column names and use it in your SELECT query, e.g.:
SELECT GROUP_CONCAT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'your_table'
AND COLUMN_NAME like 'CLNT_%';
This will give you (comma separated) column names, you can then use the result of this query to construct the SELECT query, e.g.:
SELECT <columns>
FROM your_table;
Here, you can replace <columns> with result of the first query and get the data for all the columns.
Here's MySQL documentation on INFORMATION_SCHEMA tables.
Related
I want to know how to find a nth column of a mysql table since I know that the query ->
SHOW COLUMNS FROM TABLENAME
shows all the columns but I want to fetch only selected columns. Is there a way to pass all the column names in an array and then echo the nth column like:
echo $fields[n];
I saw that this query can also be done but I do not know how to get the column numbers from this query:
SELECT * FROM Information_Schema.Columns WHERE TABLE_NAME = #tablename AND ORDINAL_POSITION = nth but it
I am using mysqli db connection method.
Is it possible to fetch all column names that have a pattern? For example, fetch all columns that start with 'allow'. I would like it to only use pure pdo query and not an php array filter.
$prepare=$database->query("show columns from TABLENAME like 'allow%'");
$fetched=$prepare->fetchAll();
Some shared hosts don't let you use the info schema, in those situations you can use show columns, which looks like this.
SHOW COLUMNS FROM `database`.`table` WHERE Field like '%access'
information_schema.columns holds column and table structure information.
$prepare=$database->query("select column_name from information_schema.columns where table_name='TABLENAME' and column_name like 'allow%'");
$fetched=$prepare->fetchAll(PDO::FETCH_COLUMN);
print_r($fetched);
Wondering if there's a way to get MySQL to return the column names when the query result returns no rows? The issue is that our system has multiple large queries sometimes:
SELECT * FROM table
SELECT table1.*, table2.field1, table2.field2
SELECT table1.field1 AS f1, SUM(table2.field1) AS f2
etc. So only way to get the column names when the returned result is empty, would be to parse the queries, and attempt to run a query on the information_schema table. Which is possible, but would be rather complex. Any ideas?
Some PHP interfaces for MySQL have a function for result set metadata, which should return information even for a result set with zero rows.
MySQLi has mysqli_stmt::result_metadata().
PDO has PDOStatement::getColumnMeta(), but it's labeled "experimental," which probably just means it's not well tested in all PDO drivers.
One technique that you can use is to create a view on the query and then query information_schema with the view name.
That is, something like this:
create view v_JustForColumns as
<your query here>;
select *
from information_schema.columns
where table_name = 'v_JustForColumns';
drop view v_JustForColumns;
Try the below one. Here replace the YOUR_SCHEMA with your actual schema name and YOUR_TABLENAME with your actual table name:
select column_name from information_schema.columns
where not exists(select * from YOUR_SCHEMA.YOUR_TABLENAME)
and table_name='YOUR_TABLENAME';
I am trying to get the column names from 2 tables.
I tried a query like: (SHOW COLUMNS FROM users) UNION (SHOW COLUMNS FROM posts) but that does not work & returns a syntax error. I tried the same query using DESCRIBE but that did not work either. How can I get all the column names from multiple tables in a single query? Is it possible?
From the docs for version 5.0 (http://dev.mysql.com/doc/refman/5.0/en/show-columns.html)
"SHOW COLUMNS displays information about the columns in a given table"
So you can't really use it on multiple tables. However if you have information_schema database then you could use it like follows:
select column_name
from `information_schema`.`columns`
where `table_schema` = 'mydb' and `table_name` in ('users', 'posts');
Here you'd have to replace the mydb with your database name, or just use DATABASE().
Yes use the information_Schema views.
SELECT * FROM information_schema.columns
WHERE Table_Name=? OR Table_name=?;
Use them as they are a standards way of querying database metadata.
If you also would like to get the name of the table column is from select table_name too
SELECT column_name, table_name
FROM `information_schema`.`columns`
WHERE `table_schema` = DATABASE() AND `table_name` in ('table1', 'table2');
I am assuming that you actually want to list all columns of the tables involved in a join.
There is a neat trick to view the qualified table and column names in a select statement. First EXPLAIN the select query, then look at the result of SHOW WARNINGS:
EXPLAIN SELECT * FROM users JOIN posts ON users.id = posts.user_id;
SHOW WARNINGS;
The result will look something like this:
Level
Code
Message
Note
1003
/* select#1 */ select `testdb`.`users`.`id` AS `id`,`testdb`.`users`.`name` AS `name`,`testdb`.`posts`.`id` AS `id`,`testdb`.`posts`.`user_id` AS `user_id`,`testdb`.`posts`.`name` AS `name` from `testdb`.`users` join `testdb`.`posts` where (`testdb`.`users`.`id` = `testdb`.`posts`.`user_id`)
The resulting query contains fully qualified name of all columns inside the select clause instead of *.
Is it possible to receive only the structure of the table even if its empty and put the field names in an array. If so which SQL command makes that possible.
If you are using MySQL 5.0 or later, you can get the field names from the INFORMATION_SCHEMA.COLUMNS table.
Something like
SELECT COLUMN_NAME
FROM COLUMNS
WHERE TABLE_NAME = <table_name>
Here is a link to a list of tables in the INFORMATION_SCHEMA database.
In Oracle and MySQL, this SQL query will give you the details of the table, including columns and column types:
describe table_name
This may or may not work in other databases.
thanks for the link the awnser is for example table 'vraag'
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'vraag'