Mysql order by data from other table - mysql

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.)

Related

How to get no of bytes occupied by a column in MySQL?

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

mysql How to convert the column of mysql select result to row

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 ?

mysql.user - list column (privileges) values as records

Need help to list all the privileges a user has (means, with "Y" on the column privileges) from the select * from mysql.user; result and have it as 1 column (as user_priv) together with host and user columns
query test user and its privileges details
Would like to turn above result to this one:
+------+------+-------------+
| user | host | user_priv |
+------+------+-------------+
| test | % | Create_priv |
+------+------+-------------+
| test | % | Reload_priv |
+------+------+-------------+
| test | % | ... |
You can get the data you are after by querying the information_schema.USER_PRIVILEGES table which derives it's data from mysql.user (documentation)
While you won't get the exact format you are after without a bit of string manipulation to separate user and host you will get more readable output for the privilege types.
e.g.
SELECT `GRANTEE` as 'USER#HOST', `PRIVILEGE_TYPE` as 'PRIVILEGE'
FROM `information_schema`.`USER_PRIVILEGES` WHERE `GRANTEE` LIKE '%paul%';
+--------------------+-------------------------+
| USER#HOST | PRIVILEGE |
+--------------------+-------------------------+
| 'paul'#'localhost' | SELECT |
| 'paul'#'localhost' | INSERT |
| 'paul'#'localhost' | UPDATE |
| 'paul'#'localhost' | DELETE |
| 'paul'#'localhost' | CREATE |
...
| 'paul'#'localhost' | DROP |
| 'paul'#'localhost' | RELOAD |
| 'paul'#'localhost' | EVENT |
| 'paul'#'localhost' | TRIGGER |
| 'paul'#'localhost' | CREATE TABLESPACE |
+--------------------+-------------------------+
If you want to know if the user can GRANT the privileges, add the column IS_GRANTABLE to your query.

In this case, why '--+' the comment style of mysql can work?

The example below comes from sqli-lab. In MySQL's doc(comment), the "-- " (double-dash followed by at least one whitespace) means a line's comment. It does work in some situation actually.
My question is how it work in the example here, why it can list all records of the 'users' table. Can you give some ideas about its mechanism? Thx!
mysql> select username, password from users where username = '' --+ '';
+----------+------------+
| username | password |
+----------+------------+
| Dumb | Dumb |
| Angelina | I-kill-you |
| Dummy | p#ssword |
| secure | crappy |
| stupid | stupidity |
| superman | genious |
| batman | mob!le |
| admin | admin |
| admin1 | admin1 |
| admin2 | admin2 |
| admin3 | admin3 |
| dhakkan | dumbo |
| admin4 | admin4 |
+----------+------------+
Actually #scaisEdge is right, the '--+' in MySQL(interactive cmdline) is NOT a comment, this usage is usually used for URLencoding(a space in a query part may be encoded to '+' or '%20').
In this case, '--+' is just 2 types of operators: plus&minus, and one - and one + offset. So this sequence is equal to:
select username, password from users where username = '' - '';
Original:
mysql> select username, password from users where username = '' --+ '';
+----------+------------+
| username | password |
+----------+------------+
| Dumb | Dumb |
| Angelina | I-kill-you |
| Dummy | p#ssword |
| secure | crappy |
...
Now:
mysql> select username, password from users where username = '' - '';
+----------+------------+
| username | password |
+----------+------------+
| Dumb | Dumb |
| Angelina | I-kill-you |
| Dummy | p#ssword |
| secure | crappy |
...
You can see the results are same.
Secondly, '' equal INTEGER 0 here.
In MySQL, any field without a valid integer will equate to 0.
mysql> select '' = 0;
+--------+
| '' = 0 |
+--------+
| 1 |
+--------+
mysql> select '0s28' = 0;
+------------+
| '0s28' = 0 |
+------------+
| 1 |
+------------+
mysql> select '8s28' = 0;
+------------+
| '8s28' = 0 |
+------------+
| 0 |
+------------+
mysql> select '8s28' = 8;
+------------+
| '8s28' = 8 |
+------------+
| 1 |
+------------+
====Type Conversion====
mysql> select '12s' + 3;
+-----------+
| '12s' + 3 |
+-----------+
| 15 |
+-----------+
mysql> select 's52s6' + 3;
+-------------+
| 's52s6' + 3 |
+-------------+
| 3 |
+-------------+
mysql> select 's8' + 3;
+----------+
| 's8' + 3 |
+----------+
| 3 |
+----------+
So '' - '' means 0 - 0 is still 0.
While the column USERNAME doesn't has one name that starts with a valid numeric character(not 0), so all the name equal 0 and match the conditon 'where username = 0'
mysql> select 'Dumb' = 0;
+------------+
| 'Dumb' = 0 |
+------------+
| 1 |
+------------+
To verify this conclusion, we can insert into a record that username starts with a integer like '4love'. You will see all records are listed except the new one.
A similar question is here:
mySQL returns all rows when field=0

how to get all enum field in a column side by side seperated by comma (,) in a mysql by query

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.