我有一个包含以下两个表的SQL数据库....
... wp_mymeta1
id | my_id | field_number | field_id | value
------------------------------------------------------------
1 | 101 | 78 | 88 | apple
2 | 287 | 76 | 55 | orange
3 | 893 | 45 | 33 | orange
4 | 922 | 23 | 33 | grape
5 | 198 | 09 | 88 | raisin
6 | 082 | 55 | 88 | apple
和wp_mymeta2 ....
my_id | date_posted
-----------------------
101 | 01-01-2014
287 | 17-01-2014
893 | 24-02-2014
922 | 18-03-2014
198 | 04-04-2014
082 | 16-04-2014
我成功地使用以下内容来计算' apple' ....
的出现次数$count = $wpdb->get_results("SELECT COUNT(*) as count FROM wp_mymeta1 WHERE value='apple'" );
我现在正试图更进一步,并计算价值' apple'的出现次数。在指定日期发布的内容。
由于date_posted存储在另一个表中,因此无法将两者联系起来。有人可以帮忙吗?
答案 0 :(得分:3)
SELECT COUNT(*) AS count
FROM wp_mymeta1 a
JOIN wp_mymeta2 b ON b.my_id = a.my_id
WHERE a.value = 'apple'
AND b.date_posted = '01-01-2014'
答案 1 :(得分:0)
试试这个:
SELECT wp_mymeta2.date_posted, COUNT(wp_mymeta1.my_id)
FROM wp_mymeta1, wp_mymeta2
WHERE value='apple' AND wp_mymeta2.my_id = wp_mymeta1.my_id
GROUP BY wp_mymeta1.my_id, wp_mymeta2.date_posted;