Jump to content

How do I combine these statements like this


gregsmith

Recommended Posts

Hi there,

I'm a semi-newb at mysql, so please bear with me.

I've got two statements that I want to combine but I can't figure out a way to do it with join or union:

[code]Statement 1: SELECT page_name, page_id, order_num FROM pages WHERE cat_id = '' AND sub_id = ''
Statement 2: SELECT cat_name, cat_id, order_num FROM categories[/code]

Here are the columns I want in the result set:

[code]page_name | page_id | cat_name | cat_id | order_num[/code]

with a field left blank if it doesn't exist in that table.

Union will combine them but obviously with the cat_name and cat_id results being listed under the page_name and page_id columns in the results set so I can't differentiate between pages and categories.

Joins seemed my best bet (I've only just looked them up) but I can't figure out how to get the results I want with the (WHERE cat_id = '' AND sub_id = '' ) bit to apply just to the pages table.  I'm guessing I can do something using pages.cat_id and pages.sub_id but my sql knowledge isn't good enough to work it out.

Any help greatly appreciated.
Link to comment
Share on other sites

Thanks for the reply.  I figured my post might be a little confusing.

It's sorta like that but order_num will never be the same between the two tables in this query.  The order_num is the cascading order in which they are going to be displayed.

I think this is what I want to do but it is returning a 'mysql_fetch_array(): supplied argument is not a valid MySQL result' style error:

[code]SELECT pages.page_id, pages.page_name, categories.cat_id, categories.cat_name, order_num FROM pages, categories WHERE pages.cat_id = '' AND pages.sub_id = ''
ORDER BY order_num ASC[/code]

This also returns the same error:

[code]SELECT * FROM pages, categories WHERE pages.cat_id = '' AND pages.sub_id = '' ORDER BY order_num ASC[/code]
Link to comment
Share on other sites

OK, I've changed the database today and I'm still having trouble putting a query together.  Hopefully it'll make a little more sense now.

I basically want to ask now:
[code]
SELECT page_name, page_id, order_num FROM pages WHERE parent_id = '0'

SELECT cat_name, cat_id, order_num FROM categories WHERE parent_id = '0'[/code]

and I want the results ordered by:

[code]ORDER BY order_num ASC[/code]

The tables are like this:

pages:

[code]page_id, parent_id, page_name, page_contents, order_num[/code]

categories:

[code]cat_id, parent_id, cat_name, order_num[/code]

The 'order_num' field denotes the order in which they'll be listed in the menu, the parent_id is the id of the container category.

I want an sql statement to provide me with a result something like this:

[table]
[tr]
[td]page_id |[/td]  [td]page_name |[/td]  [td]cat_id |[/td]  [td]cat_name |[/td]  [td]order_num |[/td]
[/tr]
[tr]
[td]10[/td]  [td]page10[/td]  [td][/td]  [td][/td]  [td]1[/td]
[/tr]
[tr]
[td][/td]  [td][/td]  [td]15[/td]  [td]cat10[/td]  [td]2[/td]
[/tr]
[tr]
[td]11[/td]  [td]page11[/td]  [td][/td]  [td][/td]  [td]3[/td]
[/tr]
[tr]
[td]12[/td]  [td]page12[/td]  [td][/td]  [td][/td]  [td]4[/td]
[/tr]
[tr]
[td][/td][td][/td][td]16[/td][td]cat_11[/td][td]5[/td]
[/tr][/table]
[table][tr][td]

I hope this makes more sense now.
Link to comment
Share on other sites

OK your query would look like so. I am not sure if you are doing this with php but here is some code. if not the sql statement can be used where ever.

[code]<?php
$sql = "SELECT * FROM pages JOIN catagories ON pages.parent_id = catagories.parent.id WHERE pages.parent_id = '0' ORDER BY order_num ASC";
  $res = mysql_query($sql) or die (mysql_error());
    while($r = mysql_fetch_assoc($res)){
extract($r);
echo "$page_id $page_name $cat_id $cat_name $order_num<br>/n";

}
?>[/code]

Ray
   
Link to comment
Share on other sites

Thanks for the reply Ray,

I am using PHP yeah.  I put the query into phpmyadmin, and first of all got:

[code]#1052 - Column 'order_num' in order clause is ambiguous [/code]

Then I took out the ORDER BY bit and it gave me a result.  It joined it all together however with stuff from the 'pages' table on the same row as the 'categories' table.  I need each row in the result to be either from the pages or the categories table, just for them to be in the right order denoted by order_num across the tables.  I'd like any unincluded columns in the row to just be blank.

Here's a snippet of what I got off of phpmyadmin:

[code]page_id  parent_id  page_name page_contents page_desc page_keywords order_num cat_id parent_id cat_name order_num
42     0         pagetest      [BLOB - 4 B] [BLOB - 0 B]   [BLOB - 0 B]     2                   23     0             asdfdfffff 6
42     0         pagetest      [BLOB - 4 B] [BLOB - 0 B]   [BLOB - 0 B]     2                   22     0             category       3
42     0         pagetest      [BLOB - 4 B] [BLOB - 0 B]   [BLOB - 0 B]     2                   21     0             cat1         1
44     0         asdfasdfffff [BLOB - 4 B]      [BLOB - 0 B]    [BLOB - 0 B]      5           23     0               asdfdfffff 6[/code]

Does this give you any other ideas?
Link to comment
Share on other sites

Well if that's the case you do not want to join the tables together you just want a query of all the blank parent_id's. You might like to know "blank" and "0" are 2 different things. you would have to physically have a 0 in the field for this query to work. if there is nothing in the field then you need to use
[code]WHERE pages.parent_id IS NULL[/code]

You would query each table seperately to just find the blank rows.

Ray
Link to comment
Share on other sites

You have to seperate the queryies for each table. You can't put them together for your case. You just want to get the rows in one table.
So if you are using phpmyadmin just query the one table and get the results for that table then do the other table.

In php just have two queries

[code]<?php
echo "<p align=center><h1>Pages Table</h1></p>";
$psql = "SELECT page_name, page_id, order_num FROM pages WHERE parent_id = '0' ORDER BY order_num ASC";
$pres = mysql_query($psql) or die (mysql_error());
while($pr = mysql_fetch_assoc($pres)){
extract($pr);
echo "$page_id, $parent_id, $page_name, $page_contents, $order_numn<br>/n";
}

echo "<p align=center><h1>Catagory Table</h1></p>";
$csql = "SELECT cat_name, cat_id, order_num FROM categories WHERE parent_id = '0' ORDER BY order_num ASC";
$cres = mysql_query($csql) or die (mysql_error());
while($cr = mysql_fetch_assoc($cres)){
extract($cr);
echo "$cat_id, $parent_id, $cat_name, $order_num<br>/n";
}
?>[/code]

Ray




Ray
Link to comment
Share on other sites

Yeah man, check the post in the php help forum.  This is exactly what I've also tried.

The fact is at some point either using an SQL statement or PHP I need to list the categories and the tables TOGETHER by order number.  This is the whole problem.  I can't figure out the best way to do it.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.