GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   WP tbl joins.. DBA'rs help (https://gfy.com/showthread.php?t=1133178)

cfU 02-10-2014 09:29 AM

WP tbl joins.. DBA'rs help
 
Am looking to return term_id, name and slug from wp_terms. These term_id's need to be associated to post_id's that reside in wp_post_meta where wp_postmeta.meta_key='wpb_post_views_count'

I also need those results ordered by : order by cast(wp_postmeta.meta_value as signed) desc;

just having problems associating back out... here are the tbls possibly involved

mysql> describe wp_postmeta;
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| meta_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| post_id | bigint(20) unsigned | NO | MUL | 0 | |
| meta_key | varchar(255) | YES | MUL | NULL | |
| meta_value | longtext | YES | | NULL | |
+------------+---------------------+------+-----+---------+----------------+


mysql> describe wp_terms;
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| term_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(200) | NO | MUL | | |
| slug | varchar(200) | NO | UNI | | |
| term_group | bigint(10) | NO | | 0 | |
+------------+---------------------+------+-----+---------+----------------+


mysql> describe wp_posts
-> ;
+-----------------------+---------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+---------------------+------+-----+---------------------+----------------+
| ID | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| post_author | bigint(20) unsigned | NO | MUL | 0 | |
| post_date | datetime | NO | | 0000-00-00 00:00:00 | |
| post_date_gmt | datetime | NO | | 0000-00-00 00:00:00 | |
| post_content | longtext | NO | | NULL | |
| post_title | text | NO | | NULL | |
| post_excerpt | text | NO | | NULL | |
| post_status | varchar(20) | NO | | publish | |
| comment_status | varchar(20) | NO | | open | |
| ping_status | varchar(20) | NO | | open | |
| post_password | varchar(20) | NO | | | |
| post_name | varchar(200) | NO | MUL | | |
| to_ping | text | NO | | NULL | |
| pinged | text | NO | | NULL | |
| post_modified | datetime | NO | | 0000-00-00 00:00:00 | |
| post_modified_gmt | datetime | NO | | 0000-00-00 00:00:00 | |
| post_content_filtered | longtext | NO | | NULL | |
| post_parent | bigint(20) unsigned | NO | MUL | 0 | |
| guid | varchar(255) | NO | | | |
| menu_order | int(11) | NO | | 0 | |
| post_type | varchar(20) | NO | MUL | post | |
| post_mime_type | varchar(100) | NO | | | |
| comment_count | bigint(20) | NO | | 0 | |
+-----------------------+---------------------+------+-----+---------------------+----------------+

mysql> describe wp_term_relationships;
+------------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------------------+------+-----+---------+-------+
| object_id | bigint(20) unsigned | NO | PRI | 0 | |
| term_taxonomy_id | bigint(20) unsigned | NO | PRI | 0 | |
| term_order | int(11) | NO | | 0 | |
+------------------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)



mysql> describe wp_term_taxonomy;
+------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------------------+------+-----+---------+----------------+
| term_taxonomy_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| term_id | bigint(20) unsigned | NO | MUL | 0 | |
| taxonomy | varchar(32) | NO | MUL | | |
| description | longtext | NO | | NULL | |
| parent | bigint(20) unsigned | NO | | 0 | |
| count | bigint(20) | NO | | 0 | |
+------------------+---------------------+------+-----+---------+----------------+

freecartoonporn 02-10-2014 09:38 AM

try here
www.stackoverflow.com

sarettah 02-10-2014 10:51 AM

Put this together real quick so don't know if it is 100% correct or not:

select c.term_id, c.name as term_name, c.slug as term_slug from wp_posts a
inner join wp_term_relationships b on a.id=b.object_id
inner join wp_term_taxonomy c on b.term_taxonomy_id=c.term_taxonomy_id
inner join wp_terms d on c.term_id=d.term_id
inner join wp_post_meta e on a.id=e.post_id
where e.meta_key='wpb_post_views_count'
order by cast(e.meta_value as signed) desc

cfU 02-10-2014 12:59 PM

Quote:

Originally Posted by sarettah (Post 19977144)
Put this together real quick so don't know if it is 100% correct or not:

select c.term_id, c.name as term_name, c.slug as term_slug from wp_posts a
inner join wp_term_relationships b on a.id=b.object_id
inner join wp_term_taxonomy c on b.term_taxonomy_id=c.term_taxonomy_id
inner join wp_terms d on c.term_id=d.term_id
inner join wp_post_meta e on a.id=e.post_id
where e.meta_key='wpb_post_views_count'
order by cast(e.meta_value as signed) desc

Awesome thank you!!!! Small changes here there.. but that was the connection I was looking for!! Thank you very much!!!
:thumbsup

fris 02-10-2014 01:02 PM

curious why you are using a sql call rather than a wp query?

sarettah 02-10-2014 01:07 PM

Quote:

Originally Posted by cfU (Post 19977356)
Awesome thank you!!!! Small changes here there.. but that was the connection I was looking for!! Thank you very much!!!
:thumbsup

You're welcome.

Quote:

Originally Posted by fris (Post 19977366)
curious why you are using a sql call rather than a wp query?

If you are asking me I just went into one of my wife's wp dbs to figure out the relationships. I am well versed in databases but not so much in wp.

If you are asking the OP, I will let him answer ;p

.

fris 02-11-2014 08:50 AM

Quote:

Originally Posted by sarettah (Post 19977374)
You're welcome.



If you are asking me I just went into one of my wife's wp dbs to figure out the relationships. I am well versed in databases but not so much in wp.

If you are asking the OP, I will let him answer ;p

.

ya the original poster ;)


All times are GMT -7. The time now is 02:50 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123