Go Back   Cloud Computing > Support > PHP Forum
 

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 03-31-11, 20:02
BOD Member
 
Join Date: Mar 2011
Posts: 19
Default Newbie and Relational query

OK I am creating a query to pull data from a Wordpress database.

I am trying to pull all POSTS (I can live with whole row from table "wp_posts" however ideally I only need 4 columns) that are (1) 'publish' and (2) '3'.

The '3' is the category of the post.

'publish' can be found in table "wp_posts.post_status" and '3' in table "wp_term_ralationships.term_taxonomy_id"

What connects them is "wp_posts.ID" = "wp_term_relationships.object_id"

I am a newbie and can handle simple, simple SELECT queries. I tried the JOIN tutorials but it's pretty over my head. Any help would be greatly appreciated. So far I have something like so, but no luck.

SELECT wp_posts.post_date, wp_posts.post_content, wp_posts.post_title, wp_posts.post_status, wp_term_relationships.object_id
FROM wp_posts,wp_term_relationships
WHERE wp_posts.ID = wp_term_relationships.object_id
ORDER BY wp_posts.post_date DSC;

Oh and this is all done in php

mysql_select_db($database_theGREENbar, $theGREENbar);
$query_recordNEWS = "SELECT wp_posts.post_date, wp_posts.post_content, wp_posts.post_title, wp_posts.post_status, wp_term_relationships.object_id
FROM wp_posts,wp_term_relationships
WHERE wp_posts.ID = wp_term_relationships.object_id
ORDER BY wp_posts.post_date DSC;";
$recordNEWS = mysql_query($query_recordNEWS, $theGREENbar) or die(mysql_error());
$row_recordNEWS = mysql_fetch_assoc($recordNEWS);
$totalRows_recordNEWS = mysql_num_rows($recordNEWS);


Redards,

Ronaldo
Reply With Quote
  #2 (permalink)  
Old 03-31-11, 20:05
BOD Member
 
Join Date: Mar 2011
Posts: 5
Default

Joins typically look something like this...

Code:
SELECT x.*
     , y.*
     , z.*
  FROM table1 x
  JOIN table2 y
    ON y.some_id = x.some_id
  JOIN table3 z
    ON z.some_id = x.some_id
 WHERE x.some_other_thing = 'a'
   AND y.some_thing_else = 'b'
   AND z.one_more_thing = 'c';
(I'll probably be reprimanded for using '*' instead of naming the columns, but hey)
__________________
Regards,
Torque
[The distance from the pivot point to the point where force is applied]
Reply With Quote
  #3 (permalink)  
Old 03-31-11, 20:07
BOD Member
 
Join Date: Mar 2011
Posts: 14
Default

Quote:
Originally Posted by Torque View Post
(I'll probably be reprimanded for using '*' instead of naming the columns, but hey)
no, we all understand that if your focus is on the joins, the SELECT clause is of minor importance

what i would reprimand you for, though, is the use of the unqualified "JOIN"

i'm a firm believer that you should indicate LEFT OUTER, INNER, RIGHT OUTER, FULL OUTER, or CROSS, and not leave it to the mysql default

in fact, in standard sql, the keyword INNER is ~not~ optional
Reply With Quote
  #4 (permalink)  
Old 03-31-11, 20:08
BOD Member
 
Join Date: Mar 2011
Posts: 19
Default

Thank you very much. Because of your help I was able to figure it out. here is what i have.


Code:
SELECT wp_posts.*, wp_term_relationships.*
FROM wp_posts JOIN wp_term_relationships on wp_term_relationships.object_id = wp_posts.ID
WHERE wp_posts.post_status = 'publish' AND wp_term_relationships.term_taxonomy_id = '3'
if this is in any way incorrect or inefficient syntax please I am always open to improving my code.
Reply With Quote
  #5 (permalink)  
Old 03-31-11, 20:10
BOD Member
 
Join Date: Mar 2011
Posts: 19
Default

Quote:
Originally Posted by UserName View Post
no, we all understand that if your focus is on the joins, the SELECT clause is of minor importance

what i would reprimand you for, though, is the use of the unqualified "JOIN"

i'm a firm believer that you should indicate LEFT OUTER, INNER, RIGHT OUTER, FULL OUTER, or CROSS, and not leave it to the mysql default

in fact, in standard sql, the keyword INNER is ~not~ optional
I dont quite follow but an explanation on what you mean here would be freat in me understanding what you mean by the terms you use and what an unqualified JOIN is?
Reply With Quote
  #6 (permalink)  
Old 03-31-11, 20:14
BoD - Chris's Avatar
Moderator
 
Join Date: May 2010
Location: at My Home ;)
Posts: 92
Send a message via Skype™ to BoD - Chris
Default

Quote:
Originally Posted by Ronaldo View Post
I dont quite follow but an explanation on what you mean here would be freat in me understanding what you mean by the terms you use and what an unqualified JOIN is?
In your query you've only used JOIN. Using INNER JOIN in place of it you will quickly be able to scan the query and know it is using an INNER JOIN rather than wondering what the default JOIN type is in MySQL.
And as "UserName" says, the standard requires that the word INNER is there, so it is a good habit to get in to.
__________________
Regard's
___________________
Chris
Windows Support Team
BODHost.com
=========================================
Linux Dedicated Server || Windows Dedicated Server
---------------------------------------------------------------------
Secure Your Data with our Off-Site Back-Up Solutions
=========================================
Follow BoDHost on Twitter
Reply With Quote
  #7 (permalink)  
Old 03-31-11, 20:18
BOD Member
 
Join Date: Mar 2011
Posts: 19
Default

Quote:
Originally Posted by BoD - Chris View Post
In your query you've only used JOIN. Using INNER JOIN in place of it you will quickly be able to scan the query and know it is using an INNER JOIN rather than wondering what the default JOIN type is in MySQL.
And as "UserName" says, the standard requires that the word INNER is there, so it is a good habit to get in to.
Thanx , I will make that adjustment
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump


All times are GMT -6. The time now is 01:11.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
Copyright © 1999-2012, BODHost Ltd. All rights reserved.