I'am create pivot table from dt_table join with dt_k
table dt_k
+------+--------+-----+
| id_k | name_k | ott |
+------+--------+-----+
| 1 | item 1 | ss |
| 2 | item 2 | ss |
| 3 | item 3 | ww |
| 4 | item 4 | dd |
| 5 | item 5 | asa |
| 6 | item 6 | rr |
+------+--------+-----+
dt_table
+------+--------+------+----+
| id_t | id_u | id_k | k |
+------+--------+------+----+
| 1 | 22 | 1 | 2 |
| 2 | 22 | 2 | 3 |
| 3 | 22 | 3 | 23 |
| 4 | 22 | 4 | 4 |
| 5 | 22 | 6 | 34 |
| 6 | 24 | 1 | 23 |
| 7 | 24 | 2 | 34 |
| 8 | 24 | 3 | 54 |
| 9 | 24 | 4 | 21 |
| 11 | 24 | 6 | 44 |
+------+--------+------+----+
I'am use pivot table to convert row to column this my code:
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when col = ''',
col,
''' then value end) as `',
col, '`')
) INTO @sql
FROM
(
select concat('', `id_k`) col
from dt_primer
) d;
SET @sql = CONCAT('SELECT s.id_s as ID,s.name_s as S, ', @sql, '
from
(
select id_s, concat('''', `id_k`) col, k value
from dt_table
) d left join dt_sat as s on s.id_s=d.id_s where d.id_s<>0
group by s.id_s');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
this code create result like
+------+------+------+------+------+------+
| id_u | op_1 | op_2 | op_3 | op_4 | op_6 |
+------+------+------+------+------+------+
| 22 | 2 | 3 | 23 | 4 | 34' |
| 24 | 23 | 34 | 54 | 21 | 44 |
+------+------+------+------+------+------+
i need this table show min and max value on each column like this
+------+------+------+------+------+------+
| id_u | op_1 | op_2 | op_3 | op_4 | op_6 |
+------+------+------+------+------+------+
| 22 | 2 | 3 | 23 | 4 | 34 |
| 24 | 23 | 34 | 54 | 21 | 44 |
| min | 2 | 3 | 23 | 4 | 34 |
| max | 23 | 34 | 54 | 21 | 44 |
+------+------+------+------+------+------+
Thanks for your help.. This resource for recent work http://sqlfiddle.com/#!9/297674/1
