find biggest value field from table mysql - 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';

Related

SQL query - I don't know all identifiers

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'

SQL query to display the length and first 3 characters of ename column in emp table

just as the question can we do something to get the length and first 3 characters of the employee name of one column
Please do not mark as answered or duplicate
i have the test tomorrow Advance SQL so I am trying to solve some imp question..
Please answer the problem
thanks again
Hi Shanu, You can use LEN() or LENGTH()(in case of oracle sql) function to get the length of a column.
SELECT LEN(column_name) FROM table_name;
And you can use SUBSTRING or SUBSTR() function go get first three characters of a column.
SUBSTRING( string, start_position, length );
SELECT SUBSTRING( column_name, 1, 3 ) FROM table_name;
To get both together use concatenation operator,
SELECT LEN(column_name)||SUBSTRING( column_name, 1, 3 ) FROM table_name;
Hope you got what you need. Any issues, feel free to ask
We can use SUBSTRING or SUBSTR() function, go get first three characters of a column.
And then try this particular query:
SELECT SUBSTRING(ename,1,3)
FROM emp;
Select len(ename) as Column_Length, left(ename,3) first_three_char from employee; ---------need to code your query. Should not use test format, will be confusing
You can also use substring function instead of left. Query will look like
Select len(ename) as Column_Length,substring(ename,1,3) first_three_char from employee;
SELECT LEN(EMPLOYEE_NAME),LEFT(EMPLOYEE_NAME,3) FROM EMPLOYEE_TABLE;

how to select dynamic columns which are result of another query in mySQl

I have two tables.
First table have column called column_name which contains values columns names of the second table like ( column1 , column2 , etc).
I need to select columns from the second table depending on the result of query column_name from the first table .
I need help solving this issue but I cant develop it:
If You work with Oracle, i suggest You to use listagg function (http://www.oracle-developer.net/display.php?id=515)
And Query will be like:
Select column_name from all_tab_columns where table_name = name_of_your_table2 and column_name in (select listagg...)
if I understand correctly.

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 statement search all tables for one specific column?

I am using the command line in MySQL on a Linux box.
I am logged in as root.
I need to find a field that in a table somewhere (in this schema there are 400+ tables)
Field is called 'discounts' or 'discounts'
I just need to find what table that specific field is in and if it is in more then one table with that feild or not.
Thanks for your help guys.
Edit
so the db is called magento and the tables are in there.
Try this:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE LOWER(COLUMN_NAME) = 'discount' or LOWER(COLUMN_NAME) = 'discounts';
Try this:
SELECT * FROM information_schema.columns WHERE column_name = 'discounts'