SQL query - I don't know all identifiers - mysql

Is there a way, where I can see every parameter or identifier I can query from my database? Not the contents but the "column names"
Something like
SELECT * FROM myDb AS String

To simply get the column names and types of a table.
You could SHOW them.
SHOW COLUMNS FROM myTable;
But if you want to know the column names of your table, and only a bit of data from it (to see what it looks like).
Then use LIMIT to get only a few records.
SELECT *
FROM myTable
LIMIT 3
It's fast and easy.
But you can also just see the columns without data if you use a criteria that's false.
SELECT *
FROM myTable
WHERE 0=1

You can also use:
show create table table_name;
but as "LukStorms" mentioned, the below statement shows you the data in table format and in a pretier way
show columns from table_name;

You can use INFORMATION_SCHEMA.COLUMNS to retrieve all columns name
select column_name from INFORMATION_SCHEMA.COLUMNS where Table_Name='Your_Table'

Related

write a query that applies to an entire db instead of a table

Is it possible to write a query that applies to an entire database as opposed to one table.
So instead of usin:
select * from table_name where columnName = ?
Can I say select * db_tables from from db where the table contains the column A?
Is is possible?
thanks
You sure can!
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME like '%A'
You cannot do SELECT * FROM all the tables, but you can run a query aganist multiple tables using other statements in SQL.

find biggest value field from table mysql

Would like to know , How do I find biggest value storing field from table .?
Table has 40 fields with string , would like to know which field has the biggest string..
Thanks
Run following query on each column.
SELECT MAX(LENGTH(column_name)) FROM table_name;
I think you are trying to find which field has biggest value. use information_schema.
Try this below exactly, and just replace your table name instead of yourtableName.
SELECT column_name, MAX(LENGTH(column_name)) FROM information_schema.columns WHERE table_name = 'yourtableName';

nesting sql statements and selecting (MySQL)

Given the following MySQL-statement:
SELECT table_name,column_name
FROM information_schema.columns
WHERE table_schema = 'myschema'
AND column_name REGEXP 'ID$'
ORDER BY table_name,ordinal_position
I would like to select from the result all columns that contain a certain ID #.
Since I don't know the exact name of the column or table I should apply my 2nd SELECT, I need to apply my SELECT to some kind of "placeholders", I think.
E.g., if the resultset of the first request is:
('wp_links', 'link_id')
('wp_options', 'option_id')
('wp_postmeta', 'meta_id')
('wp_postmeta', 'post_id')
then the select should comprise all table_name that are in the first column of the result and should take the column_name of the second column of the result as argument to test whether it contains a certain ID #.
In other words I would like to find all columns in a certain database that are named *_ID and contain a certain ID# and know their corresponding table_name the column belongs to.
You can't do what you're wanting to in SQL but you could do it using procedural SQL, either in a procedure or an anonymous block.
Create a cursor which loops through the results of your statement above. When looping through the results, construct the string you want to execute on each table to see if the desired id exists and execute the query. You will create this string by doing something like:
SET sql_statament = CONCAT("SELECT * FROM ", v_table_name," WHERE ", v_column_name," = ", v_id_number);
This tutorial should help: http://www.mysqltutorial.org/mysql-cursor/
You want to do two different things:
Get information on your database (table and column names).
Get data from your database (values from columns).
You cannot do both at the same time. You can use some programming language firing first an SQL query to get table names and columns and then build a query or several queries to get the data.

MYSQL - List specific columns

I am trying to write a query which lists the names of the columns in an SQL table, however, I don't want all the columns - just specific ones. So, for example, if I was to put the COMMENT = 'test' for the columns which I want to list then I thought my query would be:
SHOW COLUMNS FROM `tbl_name` WHERE `COMMENT`='test'
This however throws an error.
Any ideas?
Thanks,
I think you can do this using information_schema.columns:
select column_name
from information_schema.columns c
where table_name = 'tbl_name' and
column_comment = 'test';
I think that SHOW COLUMNS can't have the WHERE clause, but you can try this:
SHOW COLUMNS FROM (SELECT * FROM `tbl_name` WHERE `COMMENT`='test')

MySQL: Select multiple fields

If SQL query contains all fields what I need it will be very large.
How in SQL query I can select field from F3 to F 100 and from F150 to F200?
SELECT F3 to F100, F150 to F200 FROM database;
It is possible or not???
Tables structure change is not available
You have to :
1- manually select all columns . Or
2- do
Select * from database
And then just fetch the columns you need.
There are no shortcuts for this, you will have to list the fields needed one way or another. If the fields being selected are always the same, you should create a view for that as:
CREATE VIEW SomeView AS
SELECT
F3,
...
F100
FROM
SomeTable
and then select like:
SELECT * FROM SomeView
But again, you will have to list the fields at least once.
SELECT F3 to F100, F150 to F200 FROM database;
this query can not possible..
you must have to specify all the columns name
like select F1,f2,f3 from database;
You can't.
But if it's not possible to modify your table structure to fix the database design issue, you can use an SQL query to generate the MySQL query:
SELECT CONCAT('SELECT ', GROUP_CONCAT(COLUMN_NAME), ' FROM `your table`')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'your schema' and TABLE_NAME = 'your table'
GROUP BY TABLE_NAME
Add a filter in WHERE to select only the desired fields.