2012-05-22 6 views
0

Im는 깊은 바다에 잠시 멈추고 지식 부족에 대한 좌절감을 나타냅니다.MYSQL 쿼리는 2 개의 테이블 값을 비교합니다

그래서 필사적 인 도움을 요청할 것입니다.

이것은 더 많은 mysql 질문이지만, 현재 WP와 병행 중이므로 wp에서 시나리오를 시연합니다.

게시물 형식이 "chatt"인 게시물을 게시합니다. 각 게시물의 메타는 순결합니다. 메타 중 하나는 타임 스탬프이고 다른 하나는 채팅을위한 ID입니다.

주어진 ID (meta_value에서)와 타임 스탬프 값이 주어진 값보다 큰 모든 게시물을 가져와야합니다.

내가 지금까지이를 얻었으나, 메신저이 작업을 얻기 위해 고군분투이 어떻게 해결 해야할지

SELECT wp_postmeta.*, 
      wp_posts.* 
    FROM wp_postmeta 
LEFT JOIN wp_posts 
     ON wp_posts.ID = wp_postmeta.post_id 
      AND wp_posts.post_type = 'chatt' 
LEFT JOIN wp_postmeta t2 
     ON t2.meta_key = 'aw_chat_id' 
      AND t2.meta_value = 'test-chat-av-chattuser' 
    WHERE wp_postmeta.post_id = wp_posts.ID 
     AND t2.meta_key = 'aw_chat_timestamp' 
     AND t2.meta_value > 2 

누구나 S?

(기본적으로 모든 게시물 어디서 post_type = chatt 및 meta_value = '테스트-AV-chattuser 채팅'과 meta_value을> 2)

편집 : 구조

여기

는 wp_postmeta처럼 모습입니다 :

INSERT INTO `wp_postmeta` (`meta_id`, `post_id`, `meta_key`, `meta_value`) 
VALUES 
    (2212, 1458, 'aw_chat_id', 'test-chat-av-chattuser'), 
    (2213, 1458, 'aw_chat_timestamp', '6'), 
    (2209, 1457, 'aw_chat_id', 'test-chatt'), 
    (2210, 1457, 'aw_chat_timestamp', '5'), 
    (2207, 1456, 'aw_chat_id', 'test-chatt'), 
    (2208, 1456, 'aw_chat_timestamp', '4'), 
    (2205, 1455, 'aw_chat_id', 'test-chatt'), 
    (2206, 1455, 'aw_chat_timestamp', '3'), 
    (2202, 1454, 'aw_chat_id', 'test-chatt'), 
    (2203, 1454, 'aw_chat_timestamp', '2'), 
    (2204, 1454, 'aw_chat_answer', 'adfgsdhfgsdfhsfdh'), 
    (2199, 1453, 'aw_chat_id', 'test-chatt'), 
    (2200, 1453, 'aw_chat_timestamp', '1'), 
    (2201, 1453, 'aw_chat_answer', 'asdfasdasdf'); 



meta_id post_id meta_key meta_value 
2212 1458 aw_chat_id test-chat-av-chattuser 
2213 1458 aw_chat_timestamp 6 
2209 1457 aw_chat_id test-chatt 
2210 1457 aw_chat_timestamp 5 
2207 1456 aw_chat_id test-chatt 
2208 1456 aw_chat_timestamp 4 
2205 1455 aw_chat_id test-chatt 
2206 1455 aw_chat_timestamp 3 
2202 1454 aw_chat_id test-chatt 
2203 1454 aw_chat_timestamp 2 
2204 1454 aw_chat_answer adfgsdhfgsdfhsfdh 
2199 1453 aw_chat_id test-chatt 
2200 1453 aw_chat_timestamp 1 
2201 1453 aw_chat_answer asdfasdasdf 

그리고 wp_posts 모습 방법이 있습니다 :

INSERT INTO `wp_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) 
VALUES 
    (1458, 0, '2012-05-22 11:15:59', '0000-00-00 00:00:00', 'Chatuser chat !?', 'qwedq', '', 'draft', 'open', 'closed', '', '', '', '', '2012-05-22 11:15:59', '0000-00-00 00:00:00', '', 0, 'http://localhost/?post_type=chatt&p=1458', 0, 'chatt', '', 0), 
    (1457, 18, '2012-05-22 10:51:27', '2012-05-22 09:51:27', 'asdgasdgfsdfg', 'asdgfasdg', '', 'publish', 'open', 'closed', '', 'asdgfasdg', '', '', '2012-05-22 10:51:27', '2012-05-22 09:51:27', '', 0, 'http://localhost/?post_type=chatt&p=1457', 0, 'chatt', '', 0), 
    (1456, 1, '2012-05-22 10:48:13', '0000-00-00 00:00:00', 'sadfasdfasdfasdf', 'asdfasdfasdf', '', 'draft', 'open', 'closed', '', '', '', '', '2012-05-22 10:48:13', '0000-00-00 00:00:00', '', 0, 'http://localhost/?post_type=chatt&p=1456', 0, 'chatt', '', 0), 
    (1455, 1, '2012-05-22 10:38:47', '2012-05-22 09:38:47', 'ghkjhlkhkjhkj', 'jhgfjhfjh', '', 'trash', 'open', 'closed', '', 'jhgfjhfjh', '', '', '2012-05-22 10:38:47', '2012-05-22 09:38:47', '', 0, 'http://localhost/?post_type=chatt&p=1455', 0, 'chatt', '', 0), 
    (1454, 1, '2012-05-22 10:37:04', '2012-05-22 09:37:04', 'random text', 'awegfw', '', 'publish', 'open', 'closed', '', 'asdasd', '', '', '2012-05-22 10:37:04', '2012-05-22 09:37:04', '', 0, 'http://localhost/?post_type=chatt&p=1454', 0, 'chatt', '', 0), 
    (1453, 1, '2012-05-22 01:27:02', '2012-05-22 00:27:02', 'asdfasdfasdf', 'asdfasdf', '', 'publish', 'open', 'closed', '', 'asdfasdf', '', '', '2012-05-22 01:27:02', '2012-05-22 00:27:02', '', 0, 'http://localhost/?post_type=chatt&p=1453', 0, 'chatt', '', 0); 



    ID post_author post_date post_date_gmt post_content post_title post_excerpt post_status comment_status ping_status post_password post_name to_ping pinged post_modified post_modified_gmt post_content_filtered post_parent guid menu_order post_type post_mime_type comment_count 
1458 0 2012-05-22 11:15:59 0000-00-00 00:00:00 Chatuser chat !? askwlfoef askwlfoef  draft open closed     2012-05-22 11:15:59 0000-00-00 00:00:00  0 http://localhost/?post_type=chatt&p=1458 0 chatt  0 
1457 18 2012-05-22 10:51:27 2012-05-22 09:51:27 asdgasdgfsdfg asdgfasdg  publish open closed  asdgfasdg   2012-05-22 10:51:27 2012-05-22 09:51:27  0 http://localhost/?post_type=chatt&p=1457 0 chatt  0 
1456 1 2012-05-22 10:48:13 0000-00-00 00:00:00 sadfasdfasdfasdf asdfasdfasdf  draft open closed     2012-05-22 10:48:13 0000-00-00 00:00:00  0 http://localhost/?post_type=chatt&p=1456 0 chatt  0 
1455 1 2012-05-22 10:38:47 2012-05-22 09:38:47 ghkjhlkhkjhkj jhgfjhfjh  trash open closed  jhgfjhfjh   2012-05-22 10:38:47 2012-05-22 09:38:47  0 http://localhost/?post_type=chatt&p=1455 0 chatt  0 
1454 1 2012-05-22 10:37:04 2012-05-22 09:37:04 asd2ewfefasdf askwlfoef askwlfoef  publish open closed  askwlfoef-askwlfoef   2012-05-22 10:37:04 2012-05-22 09:37:04  0 http://localhost/?post_type=chatt&p=1454 0 chatt  0 
1453 1 2012-05-22 01:27:02 2012-05-22 00:27:02 asdfasdfasdf asdfasdf  publish open closed  asdfasdf   2012-05-22 01:27:02 2012-05-22 00:27:02  0 http://localhost/?post_type=chatt&p=1453 0 chatt  0 
+0

당신은 당신이 시도 것과로 실행중인 정확히 어떤 문제에 대한 정교한 수 있습니까? – Jeshurun

답변

1

Unles NULL 값을 포함 시키려면 LEFT JOIN이 필요하지 않습니다. 당신의 마지막 문장과 의견을 바탕으로

:

SELECT chat.*, 
      timestamp.*, 
      wp_posts.* 
     FROM wp_posts 
INNER JOIN wp_postmeta chat 
     ON wp_posts.ID = chat.post_id 
      AND chat.meta_key = 'aw_chat_id' 
INNER JOIN wp_postmeta timestamp 
     ON wp_posts.ID = timestamp.post_id 
      AND timestamp.meta_key = 'aw_chat_timestamp' 
    WHERE wp_posts.post_type = 'chatt' 
     AND timestamp.meta_value > 2 
     AND chat.meta_value = 'test-chat-av-chattuser' 
+0

흠 ... 나는 클라이언트 측에서 더 많은 정보가 있기 때문에 DB에서 특정 데이터를 얻는 방법을 알고 있습니다. 조금 민감합니다. 그러나 테이블은 모든 게시물/기사를 포함하는 wp_posts와 같으며 wp_postmeta는 각 게시물 메타 데이터를 포함합니다. wp_postmeta에는 4 cols, meta_id, post_id (wp_posts.ID와 동일), meta_key (값을 가져올 고유 식별자) 및 meta_value가 있습니다. 하나의 게시물 아래에 meta_keys가 많이있을 수 있고 여기의 경우가 있습니다. 두 개의 meta_keys가 있습니다. 그 중 하나는 정확히 있어야하고 다른 하나는 PHP에서 주어진 값보다 작아야합니다. S – Deko

+0

수정할 수정되었습니다. 그게 효과가 있다고 생각합니다. –

+0

놀라운! 정말 고맙습니다! 이제 두통 약 먹을거야 : S – Deko

관련 문제