tamilsweet
02-28-2010, 04:46 AM
Hello,
I'm trying to write a query to find maximum unique combination of two values. i.e. Posts with Tags.
This is for WordPress and if you know WordPress you need not look at the example otherwise here is what I looking for.
There are 4 tables with 1 example row. (showing only fields i'm interested in.)
terms
{ term_id, name }
10 | tag_1
term_taxonomy
{ term_taxonomy_id, term_id, taxonomy }
20 | 10 | 'post_tag'
term_taxonomy_relationships
{ object_id, term_taxonomy_id }
30 | 20
posts
{ post_id, title }
30 | post_1
post_id is the object_id
What I want is
post_id, post_title, name
and post_id and name should be unique.
Each post (post_id) can be associated with any no. of terms.
Example can be
post_id, post_title, associated terms
1 | post_1 | tag_1, tag_2, tag_3, tag_4
2 | post_2 | tag_1, tag_2, tag_3
3 | post_3 | tag_1, tag_2
4 | post_4 | tag_1
note that associated terms can be any combination, above is just an example.
Here is my query. (It could be the worst way to form query, all table have prefix wp_)
SELECT post_title, term_taxonomy_id, object_id FROM
(
SELECT post_title, object_id, term_taxonomy_id, count(*) as count
FROM wp_term_relationships
JOIN wp_posts ON object_id = ID WHERE term_taxonomy_id IN
(
SELECT term_taxonomy_id as id
FROM wp_term_taxonomy WHERE taxonomy = 'post_tag'
ORDER BY term_taxonomy_id DESC
)
AND post_type = 'post' AND post_status = 'publish'
GROUP BY term_taxonomy_id ORDER BY post_date
)
GROUP BY object_id
ORDER BY object_id DESC LIMIT 20
Any help would be really appreciated...
Regards,
Tamil
I'm trying to write a query to find maximum unique combination of two values. i.e. Posts with Tags.
This is for WordPress and if you know WordPress you need not look at the example otherwise here is what I looking for.
There are 4 tables with 1 example row. (showing only fields i'm interested in.)
terms
{ term_id, name }
10 | tag_1
term_taxonomy
{ term_taxonomy_id, term_id, taxonomy }
20 | 10 | 'post_tag'
term_taxonomy_relationships
{ object_id, term_taxonomy_id }
30 | 20
posts
{ post_id, title }
30 | post_1
post_id is the object_id
What I want is
post_id, post_title, name
and post_id and name should be unique.
Each post (post_id) can be associated with any no. of terms.
Example can be
post_id, post_title, associated terms
1 | post_1 | tag_1, tag_2, tag_3, tag_4
2 | post_2 | tag_1, tag_2, tag_3
3 | post_3 | tag_1, tag_2
4 | post_4 | tag_1
note that associated terms can be any combination, above is just an example.
Here is my query. (It could be the worst way to form query, all table have prefix wp_)
SELECT post_title, term_taxonomy_id, object_id FROM
(
SELECT post_title, object_id, term_taxonomy_id, count(*) as count
FROM wp_term_relationships
JOIN wp_posts ON object_id = ID WHERE term_taxonomy_id IN
(
SELECT term_taxonomy_id as id
FROM wp_term_taxonomy WHERE taxonomy = 'post_tag'
ORDER BY term_taxonomy_id DESC
)
AND post_type = 'post' AND post_status = 'publish'
GROUP BY term_taxonomy_id ORDER BY post_date
)
GROUP BY object_id
ORDER BY object_id DESC LIMIT 20
Any help would be really appreciated...
Regards,
Tamil