0
SELECT CONCAT(`date`,',',`viewcount`) 
FROM `stat` 
WHERE `stat`.`id` = 1 
  AND `channelstat`.`date` BETWEEN (SELECT DATE_SUB(NOW(), INTERVAL 90 DAY)) AND NOW()

This query work well and gave me result of last 2 days in CSV kind of format.

I think to generate whole csv file in MySQL. I tried to write my command to do it. When I tried GROUP_CONCAT it's not work well. Now I got rows from date less then 4 August. I am amazed because first queries make it from less then today.

SELECT GROUP_CONCAT(CONCAT(`date`,',',`viewcount`) SEPARATOR '\r\n') 
FROM `stat` 
WHERE `stat`.`id` = 1 
  AND `stat`.`date` BETWEEN (SELECT DATE_SUB(NOW(), INTERVAL 90 DAY)) AND NOW()

in second queries the last 2 line are which is totally invalid

2013-08-04,798
2013-08-

Somebody please tell me why it's not work. Why last line is different when rows are available from today to last 4 months.

The second queries have bugs like

invalid group concat or I am missing something.

2 Answers 2

1

I think that your result gets truncated by GROUP_CONCAT.

As the documentation for GROUP_CONCAT says:

The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet. The syntax to change the value of group_concat_max_len at runtime is as follows, where val is an unsigned integer:

SET [GLOBAL | SESSION] group_concat_max_len = val;
Sign up to request clarification or add additional context in comments.

Comments

0

Please try it:

$res=$mysqli->query("SELECT id,GROUP_CONCAT(client_id) as clients FROM services WHERE id = 3 GROUP BY id");
$row = $res->fetch_array(MYSQLI_ASSOC);
$result = explode(',', $row['clients']); // $row['clients'] contains string 5,6,7
$res->free();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.