xRobot
xRobot

Reputation: 26567

Can I do this with 1 query?

I am creating a simple blog with categories in php.

I want that myblog.com/category.php?id=3 show me this:

TITLE of the category 3

// other stuff

ALL POSTS of the category 3

So, actually I do 2 queries ( 1 for getting the title and 1 for getting the posts ).

Is there a way to do this with 1 query ?

Upvotes: 0

Views: 82

Answers (2)

valex
valex

Reputation: 24144

If you need all fields from these tables so I think you should use @Damien's way. If you need only titles you can use following query. In this query the first row is a title of a CATEGORY and next rows are titles of posts.

select * from
(
select 0 as ordField, categoris.Category_TITLE as Title from categoris where id =3
union all
select 1 as ordField, POSTS.Post_TITLE as Title from POSTS where category_id=3
) t order by ordField

Upvotes: 0

Damien
Damien

Reputation: 664

Depending on your database tables, you could something like that

SELECT c.title, p.data FROM category c LEFT JOIN post p ON p.category = c.category ORDER BY p.date

The category title will be repeated for every post though

Upvotes: 1

Related Questions