Forum Moderators: coopster

Message Too Old, No Replies

mySQL - LEFT JOIN "LIMIT" Question

I can't get the number of results I want

         

mek2600

3:59 am on Apr 25, 2004 (gmt 0)

10+ Year Member



I'm using PHP and mySQL in order to generate files for RSS feeds and I'm running into a problm using "LEFT JOINS" in mySQL which I'm new to.

Here's my query: "SELECT * FROM blogs LEFT JOIN comments ON blogs.blog_id = comments.blog_id ORDER BY blogs.blog_id DESC LIMIT 15"

That's getting me the last 15 rows, but I really want the last 15 blogs and their comments. I'll explain better- if I have 1 blog with 15 comments, that query is getting me the 15 comments joined with the one blog. What I'm wanting to do is get the last 15 blogs and those comments. So, I really don't know how many rows I need, but I only want 15 blogs.

Eh, hopefully that's clear enough. I know that I can do that with 2 SQL statements, but where's the fun in that? :) Really I just wish I could move the LIMIT statement somehow. Any help is appreciated.

hakre

1:32 pm on Apr 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi mek2600,

shure that would be fun to do in one query. but join creates internally a new table containing all the fields joined. so LIMIT is only able to perform on that table.

perhaps you can use expressions in LIMIT but COUNT so far does only work with GROUP BY and that's not useable with JOIN i think. that's what i've got so far.

-hakre

mek2600

4:29 am on Apr 26, 2004 (gmt 0)

10+ Year Member



Yeah, I'm going to have to do a lot more reading to figure out if I can do that in one query. I know LIMIT isnt what I really want, but I dont know how else to describe my problem.

Anyway, for now I'm cheating- I'm pulling a ton more than I really need and just using the first 15 that I really want, skipping the entries that don't fit my criteria.

Thanks for the suggestion. I'll post back here if/when I find the solution.

Woz

4:58 am on Apr 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try something like "SELECT DISTINCT TOP 15 * FROM etc."

Onya
Woz

mek2600

5:58 am on Apr 27, 2004 (gmt 0)

10+ Year Member



Aah, DISTINCT was exactly what I was looking for. Thanks a lot.