Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-17-2011, 02:29 PM   PM User | #1
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
Query 2 tables, limit results of 1 table. How?

First time I've ever done anything like querying 2 tables at a time, and I'm struggling. I have the following query
Code:
SELECT
categories.id,
categories.listing,
categories.catname,
subcategories.catid,
subcategories.subcat,
subcategories.listings,
subcategories.url
	FROM 
categories
	LEFT JOIN
subcategories
	ON
categories.id=subcategories.catid 
	ORDER BY 
categories.catname,subcategories.subcat 
	ASC
I am wanting to only show 3 results at a time from subcategories table. I tried adding LIMIT to the end of the query, but that just limited both tables.

Last edited by Nightfire; 06-17-2011 at 08:33 PM..
Nightfire is offline   Reply With Quote
Old 06-17-2011, 05:26 PM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
Two separate queries using a UNION ALL to join the queries.
guelphdad is offline   Reply With Quote
Users who have thanked guelphdad for this post:
Nightfire (06-18-2011)
Old 06-17-2011, 08:31 PM   PM User | #3
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
cheers for that
Nightfire is offline   Reply With Quote
Old 06-18-2011, 03:25 AM   PM User | #4
bazz
Master Coder

 
Join Date: Apr 2003
Location: in my house
Posts: 5,211
Thanks: 39
Thanked 201 Times in 197 Posts
bazz will become famous soon enoughbazz will become famous soon enough
you may prefer to write the query in that sort of format but you can save typing time by using an 'alias'

eg

Code:
SELECT
c.id,
c.listing,
c.catname,
s.catid,
s.subcat,
s.listings,
s.url
	FROM 
categories AS c
	LEFT JOIN
subcategories AS s
	ON
c.id=s.catid 
	ORDER BY 
c.catname,s.subcat 
	ASC
and I find this helps with 'seeing' where commas have run away and hidden somewhere.

Code:
SELECT 
  c.id
, c.listing
, c.catname
, s.catid
, s.subcat
, s.listings
, s.url
 FROM 
categories AS c
 LEFT JOIN
subcategories AS s
 ON
c.id=s.catid 
 ORDER BY 
c.catname,s.subcat 
 ASC
__________________
"The day you stop learning is the day you become obsolete"! - my late Dad.

Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
Useful MySQL resource
Useful MySQL link
bazz is offline   Reply With Quote
Users who have thanked bazz for this post:
Nightfire (06-18-2011)
Old 06-18-2011, 05:38 PM   PM User | #5
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
Cheers for that, didn't know about the alias thing
Nightfire is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:26 PM.


Advertisement
Log in to turn off these ads.