I'm a new comer to MySQL and i'm attempting to write a quite advanced query. However! Learning by doing &lifier Stackoverflow!

I possibly could most likely get all data inside a less advanced query/queries and sort the information using PHP. However I figure it may be done directly within the query.

Below is my code. Please request question if you do not understand and that i will attempt to describe better. And help me correct the code if you discover any errors.

The code will be employed to display different fields on my small wordpress pages. Different fildes may have different groups eg. sidebar-blog, sidebar-page, highlight-blog, highlight-page. It'll work similar to regular posts.

This is actually the database structure of Wordpress:

Questions:

How shall we be held supose to become listed on the tables: wordpress_posts, wordpress_term_associations, wordpress_term_taxonomy, wordpress_terms AND wordpress_postmeta?

Could it be sound practice to create advanced queries or in the event you use PHP to deal with if/else functions?

<?php

$id = $post->ID; // Gets the ID of current page

$query = "
SELECT wp_posts.post_content, wp_posts.ID, wp_terms.slug        # Data from two different tables

FROM wp_posts

# Cant figure out how to join the tables
INNER JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id

INNER JOIN wp_term_relationships
ON wp_posts.ID = wp_term_relationships.object_id

INNER JOIN wp_term_taxonomy
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id

INNER JOIN wp_terms
ON wp_term_taxonomy.term_id = wp_terms.term_id




WHERE
wp_posts.post_type = 'my-own-post-type'         # Only get specific post-type
AND
wp_posts.post_status = 'publish'                # Only get published posts


AND

# START - Only get categories specified here
(
wp_terms.slug = 'category-1'
OR
wp_terms.slug = 'category-2'
OR
wp_terms.slug = 'category-3'
OR
wp_terms.slug = 'category-4'
OR
wp_terms.slug = 'category-5'
)
# END - Only get categories specified here


AND

# I want to be able to include or exclude specific pages, even if category is the right one
# Exlude = Don't get data if current page ID is found (ID of current page will be checked in meta_value using %,$id,%)
# Include means: If include value is specifyed, then get data ONLY if ID is found (ID of current page will be checked in meta_value using %,$id,%)
# If not exclude and include are set, it should get the data

# START - Include exclude
(
# exclude is set, so check if page id match anywhere. If it IS found it it should not get the data, otherwise YES
wp_postmeta.meta_key = 'exclude' AND wp_postmeta.meta_value <> '%,$id,%'
OR
# include is set, so check if page id match anywhere. If it IS NOT found it it should not get the data, otherwise YES
wp_postmeta.meta_key = 'include' AND wp_postmeta.meta_value = '%,$id,%'
OR
# If exclude and include aren't set it should get the data
wp_postmeta.meta_key <> 'exkludera' AND wp_postmeta.meta_key <> 'include'
)
# END - Include exclude


";



$result = mysql_query($query);

while($row = mysql_fetch_array($result))
{

// collect all fields in category 1 (eg. sidebar)
if($row['slug'] == 'category-1'){
$all_fields_from_category_1 = $all_fields_from_category_1.'
<div id="field-sidebar">
'.$row['post_content'].'
</div>
';}

// collect all fields in category 1 (eg. highlight)
if($row['slug'] == 'category-2'){
$all_fields_from_category_2 = $all_fields_from_category_2.'
<div id="field-highlight">
'.$row['post_content'].'
</div>
';}



} // end while



// sidebar
echo '<div id="container-sidebar">'.
$all_fields_from_category_1.
'</div>';


// highlight
echo '<div id="container-highlight">'.
$all_fields_from_category_1.
'</div>';

?>

Latest version, new question:

SELECT DISTINCT wp_posts.post_content, wp_posts.ID, wp_terms.slug
FROM wp_posts
JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_posts.post_type = 'my-own-fields'

AND wp_posts.post_status = 'publish'
AND wp_terms.slug IN
('field1', 'field2', 'field3', 'field4', 'field5', 'field6')
AND
(
wp_postmeta.meta_key = 'exlude' AND wp_postmeta.meta_value <> '$id'
OR wp_postmeta.meta_key = 'include' AND wp_postmeta.meta_value = '$id'
OR wp_postmeta.meta_key <> 'exlude' AND wp_postmeta.meta_value <> 'include' #LAST ROW
)

The "#LAST ROW" provides me with back data even when exclude or include is placed. Thats becauce it matches two other rows within the table wordpress_postmeta.

Illustration of table wordpress_postmeta:

meta_id     post_id     meta_key    meta_value
1           30          include     18
2           30          _edit_lock  1322225789:1
3           30          _edit_last  1

If meta_key inklude or exclude cant be located for that publish_id I wish to return the information...

Example senario when I wish to return the information:

meta_id     post_id     meta_key    meta_value
2           30          _edit_lock  1322225789:1
3           30          _edit_last  1

Any concept of the way i can solve this? An argument that say: If no rows with exclude or include was discovered for current id. Return the information.

More good examples:

The publish_id is 30.

If senario is:

meta_id     post_id     meta_key    meta_value
1           30          include     18
2           30          _edit_lock  1322225789:1
3           30          _edit_last  1

Then I wish to return wordpress_posts.publish_content, wordpress_posts.ID and wordpress_terms.slug AS Lengthy AS $id IS 18.

If senario is:

meta_id     post_id     meta_key    meta_value
1           30          exclude     18
2           30          _edit_lock  1322225789:1
3           30          _edit_last  1

Then I wish to return wordpress_posts.publish_content, wordpress_posts.ID and wordpress_terms.slug AS Lengthy AS $id Isn't 18.

If senario is:

meta_id     post_id     meta_key    meta_value
2           30          _edit_lock  1322225789:1
3           30          _edit_last  1

Then I wish to return wordpress_posts.publish_content, wordpress_posts.ID and wordpress_terms.slug.