I have a mysql tableA, it changes it's fieldname every minute,i need it's fieldname,
so i do like this:
select COLUMN_NAME from information_schema.COLUMNS where table_name = 'tableA';
output:
+-------------+
| COLUMN_NAME |
+-------------+
| min |
| unkonwn1 |
| unkonwn2 |
| unkonwn3 |
| unkonwn4 |
| unkonwn5 |
| average |
+-------------+
but i need select output like this:
+-------+----------+----------+---------+------------+---------+---------+
| min | unkonwn1 | unkonwn2 | unkonwn3| unkonwn4 | unkonwn5| average |
+-------+----------+----------+---------+------------+---------+---------+
how to get this kind of result ?
Related
I need to know how many bytes are occupied by a column in MySQL.
Consider the following schema -
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id | bigint(20) unsigned | NO | PRI | NULL | |
| data | longblob | YES | | NULL | |
+-------+---------------------+------+-----+---------+-------+
Ignore id field, lets talk about data field. Consider these tuples -
+-------+---------------------+
| id | data |
+-------+---------------------+
| 1 | {ab₹} |
| 2 | {ab} |
+-------+---------------------+
So, what I need is size in bytes not no of characters like-
+---------------------+
| size_in_bytes |
+---------------------+
| 7 | // {->(1), a->(1), b->(1), ₹->(3), }->(1)
| 4 | // {->(1), a->(1), b->(1), }->(1)
+---------------------+
After hours of search I found few functions which only result in no of characters.
select OCTET_LENGTH(data) from table_name;
+--------------------+
| OCTET_LENGTH(data) |
+--------------------+
| 5 |
| 4 |
+--------------------+
SELECT LENGTH(data) from table_name;
+--------------+
| LENGTH(data) |
+--------------+
| 5 |
| 4 |
+--------------+
SELECT char_length(data) from table_name;
+-------------------+
| char_length(data) |
+-------------------+
| 5 |
| 4 |
+-------------------+
Similar Question -> How to get size of column in mysql table but none of the answers results in bytes.
How to get the sizes of the tables of a MySQL database? and this is for size of the table.
MySQL version -> 8.0
Count bits
select BIT_LENGTH (N'{ab₹}')/8;
returns 7.0000
db<>fiddle
This is a list name of tables stored a database MySql version 8.0.17
+------------------+
| listTable |
+------------------+
| Table_A2_11_2021 |
| Table_L7_12_2021 |
| Table_C3_1_2021 |
| Table_D8_10_2021 |
| Table_T0_11_2021 |
| Table_E9_3_2021 |
| Table_L4_2_2021 |
| Table_O1_12_2021 |
| Table_P2_5_2021 |
| Table_Q2_10_2021 |
| Table_A3_12_2021 |
| Table_S5_9_2021 |
| Table_T8_11_2021 |
| Table_Q6_1_2021 |
+------------------+
The table name storage policy is
Table_
Western alphabet letter (issued by an algorithm that recognizes the connected user, privileges, etc.)_
Random number_
Month Number_
Current Year
I need find on the database MySql all the table for first Western alphabet letter without Random_number for this return
+-----------------+
| listTable |
+-----------------+
| Table_A_11_2021 |
| Table_L_12_2021 |
| Table_C_1_2021 |
| Table_D_10_2021 |
| Table_T_11_2021 |
| Table_E_3_2021 |
| Table_L_2_2021 |
| Table_O_12_2021 |
| Table_P_5_2021 |
| Table_Q_10_2021 |
| Table_A_12_2021 |
| Table_S_9_2021 |
| Table_T_11_2021 |
| Table_Q_1_2021 |
+-----------------+
I have idea to use this Stored Procedure below but I just can't extract first Western alphabet letter without Random number.
CREATE DEFINER=`root`#`%` PROCEDURE `SP_SIX_MONTHS`()
BEGIN
DECLARE tyear INT(4);
DECLARE tmonth INT(2);
SET tyear = YEAR(CURDATE());
SET tmonth = MONTH(DATE_SUB(CURDATE(),INTERVAL 6 MONTH));
SET #s = CONCAT('SELECT
FROM information_schema.TABLES
WHERE table_name LIKE ''table#_',???,'%#_',tmonth,'#_',tyear,''' ESCAPE ''#'';');
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
UPDATE
return of suggestion
+------------+
| t |
+------------+
| Table_A_13 |
| Table_C_12 |
| Table_D_1 |
| Table_E_5 |
| Table_L_12 |
| Table_O_8 |
| Table_P_12 |
| Table_Q_6 |
| Table_S_14 |
| Table_T_4 |
+------------+
10 rows in set (0.12 sec)
Hmmm . . . I think this actually does what you want:
select min(listtable)
from t
group by substr(listtable, 7, 1);
This interprets "first" as "first alphabetically".
If you don't want the digit, you can remove that:
select min(insert(listtable, 8, 1, ''))
from t
group by substr(listtable, 7, 1);
Here is a db<>fiddle.
I have table 'orderby'
+-----------------+
| id | data |
+-----------------+
| 1 | 4,2,5,6 |
+-----------------+
and second table data
+-----------+
| id | ... |
+-----------+
| 2 | ... |
+-----------+
| 4 | ... |
+-----------+
| 5 | ... |
+-----------+
| 6 | ... |
+-----------+
i want to sort data table by orderby tables data column.
like this:
+-----------+
| id | ... |
+-----------+
| 4 | ... |
+-----------+
| 2 | ... |
+-----------+
| 5 | ... |
+-----------+
| 6 | ... |
+-----------+
i tried this query: select * from data order by field(id,(select group_concat(data) from orderby))
but not works.
The best solution would be to add a column orderby INT UNSIGNED to your data table, and use numbers to get the correct order.
If you need the ordering to be user dependant, you'll have to use a separate table with user id, data id (from your data table) and orderby value.
A single orderby value as you are using in your code, will not work. (It might be possible, but I think it would be hard to implement and it would not perform well.)
In mysql i need to get enum fields side by side in a column when i run a query with group by , just like as follows.
There is table as like below
mysql> describe tabex;
+---------+----------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| personid| int(11) | YES | | NULL | |
| color | enum('red','blue','white') | YES | | NULL | |
+---------+----------------------------+------+-----+---------+----------------+
there are different shirts , the column personid describes the that person id and color indicates the color of his shirt..
the data in table is as follows
mysql> select * from tabex;
+----+----------+-------+
| id | personid | color |
+----+----------+-------+
| 1 | 1 | red |
| 2 | 1 | white |
| 3 | 2 | blue |
| 4 | 2 | red |
+----+----------+-------+
4 rows in set (0.00 sec)
when i ran a query i am getting results like this
mysql> select personid , color from tabex group by personid;
+----------+-------+
| personid | color |
+----------+-------+
| 1 | red |
| 2 | blue |
+----------+-------+
but i want the result like below
+----------+-------------+
|personid | color |
+----------+-------------+
|1 | red,white |
|2 | blue,red |
| | |
+----------+-------------+
how can i get the result as above by using group by and aggregation (if any for enum).
that is here i want to get the result for enum fields as like we will get by using count or sum functions and group by .
The GROUP_CONCAT() aggregate function does what you want:
SELECT personid, GROUP_CONCAT(color) colors
FROM tabex
GROUP BY personid
This works with any kind of field, not just ENUM.
I have a database named bbs, which have 37 tables. I want to find all columns in these tables where the column name length is greater than 5!
mysql> show tables;
+---------------------+
| Tables_in_rails_bb |
+---------------------+
| articles |
| articles_categories |
| bookmarks |
| categories |
| comments |
| drafts |
| extension_groups |
| extensions |
| forum_tracks |
| forums |
| icon_items |
| icons |
| levels |
| management_groups |
| management_logs |
| message_folders |
| message_tos |
| messages |
| moderators |
| posts |
| replies |
| reports |
| roles |
| roles_users |
| schema_migrations |
| sessions |
| smiles |
| subscribes |
| system_configs |
| topic_tracks |
| topics |
| upload_files |
| users |
| users_forums |
| users_topics |
| warnings |
| word_replacements |
+---------------------+
37 rows in set (0.25 sec)
How to write the sql?
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE CHAR_LENGTH(COLUMN_NAME) > 5
AND TABLE_SCHEMA='YourDatabase';
Not tested! Found this question and edited the query. Something like this should at least get you started :)
The only thing you will change here is the name of your database (Table_Name, Column_Name are fixed). try this one:
SELECT Table_Name, Column_Name
FROM information_schema.columns
WHERE table_schema = 'databaseName' -- <= Database Name Here
HAVING CHAR_LENGTH(COLUMN_NAME) > 5
ORDER BY Table_Name, Column_Name
or you can also select all fields
SELECT *
FROM information_schema.columns
WHERE table_schema = 'databaseName' -- <= Database Name Here
HAVING CHAR_LENGTH(COLUMN_NAME) > 5
ORDER BY Table_Name, Column_Name
Just query the information_schema database:
mysql> connect information_schema;
mysql> select table_name, column_name from columns where table_schema = 'bbs' and char_length(column_name) > 5;
Take into account that char_length(str) will give you the amount of characters str have while length(str) will result in the size in bytes of str.