I have a SQLite table with columns like these:
id, name_1, name_2, name_nick, phone, email
and I am looking for a way to search in all columns which begins with name_.
Like LIKE but for the column names.
I find a lot for MySQL etc, but nothing for SQLite.
These queries do not work as I want:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'Customers'
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'Foods'
AND table_schema = 'YourDB'
AND column_name LIKE 'Vegetable%'
I get this:
ERROR: no such table: information_schema.columns
I have seen this question, which is how to obtain column names using SQL. However I think I would need a scripting language to run the query to get column names, filter them in some fashion, and then construct a new query using the filtered set of names. Instead I would like to try to do this all in SQL, if it is possible.
Related
I like to build a query which can search in complete data base in all tables.
Ex.
Search String: 9049
Now in result I need search result from all table where this string matches.
Simply I can't write LIKE to all field of table.
Thanks In Advance.
You could do an SQLDump of the database (and its data) then search that file.
or
You can peek into the information_schema table. It has a list of all tables and all fields that are in a table. You can then run queries using the information that you have gotten from this table.
You could first read out all the tables:
SELECT table_name FROM information_schema.tables WHERE table_schema = 'yourdatabasename'
and then loop through all the tables and get all the columns:
SELECT column_name FROM information_schema.columns
WHERE table_schema = 'yourdatabasename' AND table_name = 'thetablenamefromyourloop'
and then build a dynamic statement thats builds the WHERE clause based on your searchterm.
or to get just all the information just read out everything. This contains all the information about all the tables and columns, which might be useful to just search in all the varchar and text fields etc.
SELECT * FROM information_schema.columns
WHERE table_schema = 'yourdatabasename'
I'm having to query from a Database that has more than 50 tables - all having the same structure (I know, Horrid Database design from a legacy project that's been in production for 5+ years!). To do this, I've queried the information_Schema like below:
SELECT
TABLE_NAME
FROM
INFORMATION_SCHEMA.tables
WHERE TABLE_SCHEMA =
'projectdatabase'
AND
TABLE_NAME LIKE '%_usertable'
Which provides me the 50 or so tables that I need, in a result. Now, I will need to query columns from within each of those tables for example, PRODUCT_ID. In doing so, I've attempted:
SELECT
projectdatabase.userTable.PRODUCT_ID
FROM (
SELECT
TABLE_NAME as userTable
FROM
INFORMATION_SCHEMA.tables
WHERE TABLE_SCHEMA =
'projectdatabase'
AND
TABLE_NAME LIKE '%_usertable'
) AS userTables
Now this obviously doesn't work due to MySQL not treating the 'userTable' as Database table - but what I'm attempting to do, is query * FROM {tablename} where tablename was the information_schema query result.
I could attempt to split this up in PHP, although I'm eagerly wondering if this was possible to do within MySQL.
You have to use prepared statement:
SET #sql:=(SELECT GROUP_CONCAT(
CONCAT("SELECT PRODUCT_ID FROM ", TABLE_NAME) SEPARATOR " UNION ")
FROM INFORMATION_SCHEMA.tables
WHERE TABLE_SCHEMA = 'projectdatabase'
AND TABLE_NAME LIKE '%_usertable');
PREPARE stmt FROM #sql;
EXECUTE stmt;
EDIT: You can also set SET SESSION group_concat_max_len = 1000000;, but doing all in SQL is not a thing to do. Your table list is kind of a constant, and the query would be in a better place in your PHP code.
Adam deserves the bounty. The group_concat_max_len can be captured and restored if you like. And you can make it even larger. So I don't see a reliability issue. And I agree that you cannot have the tablename be a 'variable'.
The PHP equivalent would go something like:
Get the list of tables from
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.tables
WHERE TABLE_SCHEMA = 'projectdatabase'
AND TABLE_NAME LIKE '%_usertable';
Either build the UNION, Adam's suggestion, or run the 50 SELECT's, each based on one tablename from the above list. Then combine them however you want.
Recommend thinking through UNION DISTINCT versus UNION ALL.
How do you print out all the column names of a table. Just like a simple, for loop type printing out. I'm trying a wacky thing and checking against the column names will be useful.
This may sound like a similar question to others, but either I'm too stupid or I just haven't found my answer.
Simplifcation - If there are 3 columns in the table, I want to just print them out like
column1
column2
column3
More Explanation I should have added before. I'm using php to run a website that stores data into a database. I want to try a theory that involves using the column names of a table in order to change the values in that table.
#Michael -
$SQL = "SELECT column_name from information_schema.columns where
table_schema = "db1.com" and table_name = "users" ";
Thank you all for your help. However I have given up on trying this method. Thank you for all the responses
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='yourdatabasename'
AND `TABLE_NAME`='yourtablename';
SELECT column_name from information_schema.columns where table_schema = "your_database_name" and table_name = "your_table_name";
Actually the answer is the longer one. The right answer for this is describe {table_name}
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 *.
As you guys propably know, there's the possibility to add a comment to a column in MySQL.
Now I was wondering how I could obtain this comment via PHP/MySQL.
I was searching the web but I didn't find any solution yet.
Do you guys have any idea/solution for this problem?
Greetings!
SELECT
COLUMN_COMMENT
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_SCHEMA = 'db-name' AND
TABLE_NAME = 'table-name' AND
COLUMN_NAME = 'column-name'
http://dev.mysql.com/doc/refman/5.0/en/columns-table.html
Just use this SQL:
SHOW FULL COLUMNS FROM myTable
http://dev.mysql.com/doc/refman/5.0/en/show-columns.html
The FULL keyword causes the output to include the column collation and comments, as well as the privileges you have for each column.
Data regarding MySQL's tables in stored in the information_schema views.
You should be able to get it from there. This requires root privileges.
SELECT table_schema, table_name, column_comment
FROM INFORMATION_SCHEMA.`columns` c
WHERE c.table_schema = 'mydatabase'
AND c.table_name = 'mytable'
AND c.column_name = 'myfield'
You can fetch those metadata from the information_schema database.
Ex:
SELECT column_name, column_comment FROM information_schema.columns WHERE table_name = 'user'
Where user is your table name.
If you have correct privileges you could make this query:
$query = "SHOW FULL COLUMNS from node;";
$result = mysql_query($query);
And then fetch the results (there is a column named Comment that holds the comments)
Use the SQL command SHOW FULL COLUMNS as described in the MySQL manual. Its output contains the comments.