You might want to use the following query.
SELECT CONCAT('You have ',
COUNT(`read`),
' unread msg to your number ',
customer,
' customer') AS news
FROM msg
WHERE `read` = '0' AND `sent_to` = '45'
GROUP BY customer;
Note that this readis a reserved word in MySQL, so you need to enclose it in backlinks. ( Source )
Test case:
CREATE TABLE msg (
`sent_to` int,
`customer` int,
`msg` varchar(10),
`read` int
);
INSERT INTO msg VALUES(45, 3, 'bla', 0);
INSERT INTO msg VALUES(34, 4, 'bla', 1);
INSERT INTO msg VALUES(34, 6, 'bla', 0);
INSERT INTO msg VALUES(45, 3, 'bla', 0);
INSERT INTO msg VALUES(56, 7, 'bla', 1);
INSERT INTO msg VALUES(45, 8, 'bla', 0);
Request Result:
+-------------------------------------------------+
| news |
+-------------------------------------------------+
| You have 2 unread msg to your number 3 customer |
| You have 1 unread msg to your number 8 customer |
+-------------------------------------------------+
2 rows in set (0.00 sec)
source
share