Вот мои данные таблиц
Билеты
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ id | tic_files |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 1 | 56456sasd.jpg,asd4455asd.jpg,asd564asd5.txt,asd564asd.css |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 2 | 56a4sdasd.txt,jkasd5ass.jpg |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 3 | |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 4 | asdjhagsd.gif,spsnd65asd.php,56a4sd54asd.txt |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Комментарии
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ id | tic_id | com_files |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 1 | 1 | 56456sasd.jpg,asd4455asd.jpg,asd564asd5.txt |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 2 | 3 | 56a4sdasd.txt,jkasd5ass.jpg |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 3 | 1 | sdf54sdf.gif,swrsdf54sdf.rar |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 4 | 2 | asdjhagsd.gif,spsnd65asd.php,56a4sd54asd.txt |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Вот моя проблемаИ мои попытки
Я хочу, чтобы результат был таким
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ tic_id | count(tic_files) | count(com_files) |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 1 | 4 | 5 |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 2 | 2 | 3 |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 3 | 0 | 2 |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 4 | 3 | 0 |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Я пытался сделать это
Присоединиться к билетам с group_concat (комментарии) иполучить данные как-то так:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| tic_id | tic_files | com_files |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| 1 | 56456sasd.jpg,asd4455asd.jpg,asd564asd5.txt,asd564asd.css | 56456sasd.jpg,asd4455asd.jpg,asd564asd5.txt,sdf54sdf.gif,swrsdf54sdf.rar |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| 2 | 56a4sdasd.txt,jkasd5ass.jpg | asdjhagsd.gif,spsnd65asd.php,56a4sd54asd.txt |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| 3 | | 56a4sdasd.txt,jkasd5ass.jpg |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| 4 | asdjhagsd.gif,spsnd65asd.php,56a4sd54asd.txt | |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Но я постараюсь использовать больше, чем group_concat (ограничение на всех серверах составляет 1024 КБ), поэтому я хочу получить разделитель между файлами ","
Решено
Это мой запрос
SELECT
cat_id as cat__id ,
cat_name,(
SELECT count(tickets.tic_id) from tickets INNER JOIN cats on(tickets.tic_cat = cats.cat_id) where tickets.tic_cat = cat__id group by tickets.tic_cat limit 1
) as "count(tickets)",(
SELECT ( LENGTH( GROUP_CONCAT(tickets.tic_files)) - LENGTH( REPLACE( GROUP_CONCAT(tickets.tic_files ) , "," , "" ) ) + 1 ) FROM tickets
INNER join cats on ( tickets.tic_cat = cats.cat_id )
WHERE tickets.tic_files != "" AND tickets.tic_cat = cat__id
LIMIT 1
) as "count(ticket_files)",(
SELECT GROUP_CONCAT(tickets.tic_files) FROM tickets
inner join cats on ( tickets.tic_cat = cats.cat_id )
WHERE tickets.tic_files != "" AND tickets.tic_cat = cat__id
LIMIT 1
) as "tickets_files",
COUNT(comments.tic_id),(
SELECT ( LENGTH( GROUP_CONCAT(comments.com_files)) - LENGTH( REPLACE( GROUP_CONCAT(comments.com_files ) , "," , "" ) ) + 1 ) FROM comments
INNER join tickets on ( tickets.tic_id = comments.tic_id )
INNER join cats on ( tickets.tic_cat = cats.cat_id )
WHERE comments.com_files != "" AND comments.tic_id = tickets.tic_id and tickets.tic_cat = cat__id
LIMIT 1
) as "count(com_files)",(
SELECT GROUP_CONCAT(comments.com_files) FROM comments
INNER join tickets on ( tickets.tic_id = comments.tic_id )
inner join cats on ( tickets.tic_cat = cats.cat_id )
WHERE comments.com_files != "" AND comments.tic_id = tickets.tic_id and tickets.tic_cat = cat__id
LIMIT 1
) as "com_files"
from tickets
INNER JOIN cats ON (tickets.tic_cat = cats.cat_id)
INNER JOIN comments ON ( comments.tic_id = tickets.tic_id )
group by tickets.tic_cat
и вот изображение результата