SQL query help [Archive] - ForumsHQ
Posted: Tue Aug 23, 2005 12:16 pm
I'm working on a SQL (mysql used) query and I'm having some issues constructing it. I've been looking at it for a few hours which is too much time. Hopefully I can get some help on here.
I have 2 tables, one is `sites` and the other `posts`. There are MANY `posts` per `sites`. Here's how they're constructed:
mysql> desc posts;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | | PRI | NULL | auto_increment |
| site_id | int(11) | | | 0 | |
| title | varchar(50) | | | | |
| description | text | | | | |
| content | text | | | | |
| date | int(11) | | | 0 | |
| link | varchar(120) | | | | |
+-------------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
and
mysql> desc sites;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | | PRI | NULL | auto_increment |
| rss_url | varchar(100) | | | | |
| title | varchar(40) | | | | |
| interval | int(11) | | | 0 | |
| lastupdated | int(11) | | | 0 | |
+-------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
so `posts` has a foreign key that leads to the `sites` table.
The results of the query I wish to construct would be as follows:
5 (or some X number specified) posts PER site for every site.
The closest query I can construct is as follows:
SELECT s.rss_url, s.title, p.title AS post_title, p.description
FROM sites s, posts p
WHERE p.site_id = s.id
ORDER BY s.title
LIMIT 0,5
But I'm trying to make it so that there's 5 posts for each site, instead of 5 results total.
Any ideas?
I have 2 tables, one is `sites` and the other `posts`. There are MANY `posts` per `sites`. Here's how they're constructed:
mysql> desc posts;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | | PRI | NULL | auto_increment |
| site_id | int(11) | | | 0 | |
| title | varchar(50) | | | | |
| description | text | | | | |
| content | text | | | | |
| date | int(11) | | | 0 | |
| link | varchar(120) | | | | |
+-------------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
and
mysql> desc sites;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | | PRI | NULL | auto_increment |
| rss_url | varchar(100) | | | | |
| title | varchar(40) | | | | |
| interval | int(11) | | | 0 | |
| lastupdated | int(11) | | | 0 | |
+-------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
so `posts` has a foreign key that leads to the `sites` table.
The results of the query I wish to construct would be as follows:
5 (or some X number specified) posts PER site for every site.
The closest query I can construct is as follows:
SELECT s.rss_url, s.title, p.title AS post_title, p.description
FROM sites s, posts p
WHERE p.site_id = s.id
ORDER BY s.title
LIMIT 0,5
But I'm trying to make it so that there's 5 posts for each site, instead of 5 results total.
Any ideas?