MYSQL查询比较2个表值
我现在在深水区,因为最后期限和缺乏知识而感到沮丧 所以我会寻求绝望的帮助 这是一个更复杂的问题,但由于我目前正在与WP合作,我将从WP演示这个场景 我有“chatt”类型的帖子,其中每个帖子都有它的元元素。 其中一个元是时间戳,另一个是聊天的id 我需要获得所有具有给定ID(来自meta_值)且时间戳值大于给定值的帖子 到目前为止,我已经做到了,但我正在努力让它发挥作用:SMYSQL查询比较2个表值,mysql,wordpress,Mysql,Wordpress,我现在在深水区,因为最后期限和缺乏知识而感到沮丧 所以我会寻求绝望的帮助 这是一个更复杂的问题,但由于我目前正在与WP合作,我将从WP演示这个场景 我有“chatt”类型的帖子,其中每个帖子都有它的元元素。 其中一个元是时间戳,另一个是聊天的id 我需要获得所有具有给定ID(来自meta_值)且时间戳值大于给定值的帖子 到目前为止,我已经做到了,但我正在努力让它发挥作用:S SELECT wp_postmeta.*, wp_posts.* FROM wp_
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
有人知道如何解决这个问题吗
(基本上,获取所有帖子,其中post_type=chatt,meta_value='testchat av chattuser'和meta_value>2)
编辑:结构
以下是wp_Posteta中的情况:
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_帖子的样子:
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
除非希望包含空值,否则不需要左连接 根据你的最后一句话和评论:
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'
你能详细说明一下你所做的尝试到底遇到了什么问题吗?嗯。。。我真的不知道我是如何从数据库中获取特定数据的,因为客户端有很多数据,这有点敏感。但是这些表类似于包含所有帖子/文章的wp_posts和包含每个帖子元数据的wp_posteta。在wp_posttea中有4个col,meta_id、post_id(与wp_posts.id相同)、meta_key(获取值的唯一标识符)和meta_value。在一篇文章中,可能有很多meta_键,就是这样,我得到了两个meta_键,其中一个需要精确,另一个需要小于PHP中给定的值:SAmazing!非常感谢你!现在我要吃一片头痛药