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'
Related
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.
How to list all tables in MySQL database which match like "bl_pelanggan"+YEAR?
currently I use the following query:
SHOW TABLES LIKE 'bl_pelanggan%'
but it list all of these:
I want only inside the red box, how to do it?
You can try something like this,
SELECT table_name, table_type, ENGINE
FROM information_schema.tables
WHERE table_schema = 'your schema name' AND table_name REGEXP '[[:digit:]]$'AND table_name LIKE 'bl_pelanggan%'
ORDER BY table_name;
SHOW TABLES LIKE 'bl_pelanggan____'
This should also be possible:
show tables from <your_schema_name> where tables_in_<your_schema_name> like "bl_pelanggan201%";
SHOW TABLES FROM INFORMATION_SCHEMA
WHERE Tables_in_information_schema LIKE 'bl_pelanggan201%'
You can check http://rextester.com/AOVZ31733
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);
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.
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'