2010-04-21 2 views
3
여기

은 ID 번호 45 로그를 예를 사용자의 MySQL의쉽게 MySQL의 쿼리 질문

sent_to customer msg  read 
------- -------- ------ ----- 
45   3  bla  0 
34   4  bla  1 
34   6  bla  0 
45   3  bla  0 
56   7  bla  1 
45   8  bla  0 

에서 "MSG"테이블,

내가 그를이보고 싶어입니다

you have 2 unread msg to your "number 3" customer 
you have 1 unread msg to your "number 8" customer 

은 (는) 뉴스 피드를 좋아합니다.

어떤 검색어를 사용해야합니까?

들으

답변

1

. 당신이 역 따옴표로 묶어야 할 수 있도록 read는, MySQL은 예약어입니다

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; 

참고. (Source)

테스트 케이스 :

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

질의 결과 :

+-------------------------------------------------+ 
| 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) 
+0

위대한 답변을 주셔서 감사합니다 및 또한 "읽기"에 대한 정보를 제공, 나는 문제가있다, mysql_num_rows는 0 비록 내가 "읽기 = 0"데이터베이스에 있습니까? –

+0

오 그래, 내가 그걸 고쳤어. –

0

... 다음과 같은 쿼리를 사용할 수 있습니다

select count(*), customer from msg where read = 0 group by customer 
+0

과'아마도 sent_to'? –

+0

가장 확실! –

1
SELECT COUNT(read), customer FROM msg WHERE read = 0 AND sent_to = '45' GROUP BY customer; 
1
select count(sent_to), customer 
from msg 
where read < 1 
and sent_to = 45 
group by customer