scarhand
scarhand

Reputation: 4337

seemingly simple join i cant figure out

ive never done a join like this before

basically, i have 2 tables with the following structures:

categories:
- id - int
- name - varchar
- show_in_menu - varchar
- sort_order - int

pages:
- id - int
- name - varchar
- show_in_menu - varchar
- sort_order - int

im trying to write a query that will select all categories and pages and spit them out in a list so that they are ordered by the sort_order field, and i only want to select the rows where show_in_menu == 'Yes'. the problem is i want this to be ordered by sort_order within both tables, example:

page 1 - (sort order of 0)
page 2 - (sort order of 1)
category 1 - (sort order of 2)
page 4 - (sort order of 3)
category 7 - (sort order of 4)

i really have no clue how to finish my query, here it is:

select c.*, p.* 
from `categories` as c
join `pages` as p

any help would be greatly appreciated...

Upvotes: 2

Views: 45

Answers (1)

Zach Lesperance
Zach Lesperance

Reputation: 347

You could try a Union query:

SELECT * FROM categories
WHERE show_in_menu = 'Yes'
UNION
SELECT * FROM pages
WHERE show_in_menu = 'Yes'
ORDER BY sort_order

http://w3schools.com/sql/sql_union.asp

Upvotes: 1

Related Questions