I'm writing a simple forum system to go along with one of my projects - nothing fancy, just categories, forums, threads, and posts. Some threads, however, are marked as 'stick', which means that they're stickies. No matter how many threads there are or where they're posted on, they always must appear at the top of the forum. However, the other threads are paginated.
I've tried using the following mysql query to select 20 normal threads and all stickies, but it doesn't work. Is there a way to get around this in the actual query, or should I resort to using multiple queries in the PHP to get the other threads?

SELECT * FROM `threads` WHERE `forumid` = 1 AND (`status` = 'none' LIMIT 20) OR (`status` = 'stick')

Thanks! (This is in the PHP forum as I'm not sure if the solution is in PHP or MySQL)

Recommended Answers

All 2 Replies

(This is in the PHP forum as I'm not sure if the solution is in PHP or MySQL)

It's MySQL, but no problem :)
How about using an union?

SELECT * FROM `threads` WHERE `forumid` = 1 AND `status` = 'stick'
UNION
SELECT * FROM `threads` WHERE `forumid` = 1 AND `status` = 'none' LIMIT 20

Thank you! :D That really helped me! :) I have this worked out now. Thanks again for helping me. x3

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.