473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

MySQL list, relative to one post.

Ok, we'll see if anyone here has an idea for this problem.

I have a database of texts in a blog system. The database is MySQL. The
database contains blogs for hundreds of different "bloggers" and different
categories.

So, to list a specific posters blogs in a specific category, it looks something
like this:

select id, headline from blogs where member = 1234 and category
= 'Technology' order by date desc limit 20

So far so good. That displays the 20 latest blogs that fit that, which could
return something like this:

1256 My Mac!
1034 I ordered a Mac
945 I like my Palm

And so on.

Now, my problem arises when someone searches for blog entries for this person
and finds a blog entry that is older than the 20 ones listed as most recent
blogs. Let's say I am reading "12 Maybe I'll buy a Palm", and there are fifty
entries between 12 and 945 that won't be shown.

Now, I dont' want to extend my "limit" clause to go all the way down to 12,
since that could potentially result in thousands of lines returned.

What I *DO* want to do is to have the list be relevant to the current text. So
if the limit is 5, and the asterix is the current one, different lists might
look like this:

1. 1256 My Mac!
* 2. 1034 I ordered a Mac
3. 945 I like my Palm
4. 924 Quad Mac!
5. 900 Cool stuff for christmas
And if I click the fifth item (christmas), the list shifts to:

3. 945 I like my Palm
4. 924 Quad Mac!
* 5. 900 Cool stuff for christmas
6. 845 How about that new Palm?
7. 840 Gadgets galore

And if I somehow end up reading the very first blog entry for this person:

23. 101 Don't you just hate MS?
24. 98 If I only had the money...
25. 78 Hardcore gaming... on a palm!?
26. 24 Mystified IT
* 27. 12 Maybe I'll buy a Palm

Do you see where I'm getting at?

As far as I know, I have to do this in two SQL queries - one that fetches all
posts before the current one and one that fetches all the ones after it. But do
any of you guys have a smart SQL qquery that will fetch it correctly for me in
one pass?
--
Sandman[.net]
Nov 22 '05 #1
4 1144
> have a smart SQL qquery that will fetch it correctly for me in one pass?

If you are fairly sure that the IDs are sequential, you may try "where
id<CURRENTID+2 OR id>CURRENTID+2 (my brain isn't working logicly now...
you may need to flip the + and - signs)

If they aren't sequential, make an educated guess about how
insequential they are and grab 10 or so on each side and filter out all
but the five you want in your script. It is a lot faster to grab a lot
of stuff in one query and filter it in your script than to grab a
little stuff in two or more queries.

Hope this gives you some ideas.

Nov 22 '05 #2
In article <11*********************@g44g2000cwa.googlegroups. com>,
"Joshie Surber" <jo**********@gmail.com> wrote:
have a smart SQL qquery that will fetch it correctly for me in one pass?
If you are fairly sure that the IDs are sequential


Well, had you looked at my example data, you would have seen they are not. :P
If they aren't sequential, make an educated guess about how
insequential they are and grab 10 or so on each side and filter out all
but the five you want in your script. It is a lot faster to grab a lot
of stuff in one query and filter it in your script than to grab a
little stuff in two or more queries.


"lots of stuff" could mean tens of thousands of posts, so that's not a good
idea.

what I would like is something like this:

select * from blog where member = 12 and category = 'Sports' and id = 12345
limit -5, 5;



--
Sandman[.net]
Nov 22 '05 #3
Sandman wrote:

what I would like is something like this:

select * from blog where member = 12 and category = 'Sports' and id = 12345
limit -5, 5;


SELECT * FROM blog
WHERE ... AND id < $currentID
ORDER BY id DESC
LIMIT 5
UNION
SELECT * FROM blog
WHERE ... AND id >= $currentID
ORDER BY id
LIMIT 6

Second limit needs to be 6 to make sure the current ID is included.

--
Oli

Nov 22 '05 #4
In article <11**********************@z14g2000cwz.googlegroups .com>,
"Oli Filth" <ca***@olifilth.co.uk> wrote:
Sandman wrote:

what I would like is something like this:

select * from blog where member = 12 and category = 'Sports' and id =
12345
limit -5, 5;


SELECT * FROM blog
WHERE ... AND id < $currentID
ORDER BY id DESC
LIMIT 5
UNION
SELECT * FROM blog
WHERE ... AND id >= $currentID
ORDER BY id
LIMIT 6

Second limit needs to be 6 to make sure the current ID is included.


Whoa - that looks like it's something!

But that will actually run two queries in the SQL-server, right? But still, I
get both in the same pass. I'll try it out. Thanks!
--
Sandman[.net]
Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: aaron | last post by:
I have a question about (i think) joining. If I have a table in a database that has this info: key - name - favorite 1 - john - 2 2 - judy - 3 3 - joe - 1 the favorite icecream...
11
by: George Augustino | last post by:
REQUEST FOR DISCUSSION (RFD) unmoderated group comp.databases.mysql Newsgroup line: comp.databases.mysql MySQL relational database system discussion group. This is a formal Request For...
0
by: Girish Agarwal | last post by:
--0-474210375-1058976151=:31789 Content-Type: text/plain; charset=us-ascii Content-Id: Content-Disposition: inline Note: forwarded message attached. __________________________________
0
by: Mark Adams | last post by:
I am a relative newbie to MySQL. I had a Postfix+Courier+MySQL mail server running for several months. It took me a week or so to get it up and running in September. Now, I did a clean upgrade to...
0
by: Mark Adams | last post by:
I am a relative newbie to MySQL. I had a Postfix+Courier+MySQL mail server running for several months. It took me a week or so to get it up and running in September. Now, I did a clean upgrade to...
0
by: Mark Adams | last post by:
I am a relative newbie to MySQL. I had a Postfix+Courier+MySQL mail server running for several months. It took me a week or so to get it up and running in September. Now, I did a clean upgrade to...
5
by: gooderthanyou | last post by:
Alright i've looked a ton of topics and most of them are unresolved... configure: error: Cannot find MySQL header files under usr/local/mysql. Note that the MySQL client library is not bundled...
13
by: Casimir Pohjanraito | last post by:
I have a list of links, with a thumbnail image hidden(resized) next to the link. Complete html&css at end of this post. CSS for the link resizes the image on a:hover. All is good, except the...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.