If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > combine 2 queries into 1

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-18-08, 09:36
dicanio dicanio is offline
Registered User
 
Join Date: Nov 2008
Posts: 17
Question combine 2 queries into 1

Hi,

i have 2 tables , a table for my articles and a table for categories !

i need to show 5 latest posts in articles for each cat in a page

for now i get cat names by this query :
Code:
select c_id,c_name from _cats order by c_name
loooooop

Print the name : c_name

now , i loop in the query and i get articles by this query :

Code:
select TOP 5 a_title from _articles where a_cat = prevquery.c_id order by a_date desc
Print article : a_title

end looooop

now i need to combine these 2 queries in one query .

i tried to combine group by and LIMIT but no results , any suggestion ?
also i cannot use LIMIT in subquery ! the i am complicated !
Reply With Quote
  #2 (permalink)  
Old 11-18-08, 09:55
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Have you tried the SQL Server solution provided here: prevent loop ?
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 11-18-08, 11:25
dicanio dicanio is offline
Registered User
 
Join Date: Nov 2008
Posts: 17
georgev , yes , the sql server solution may work but i asked this question by mistake in mssql server section , and i found the solution in mssql server , we are developing the application in mssl and mysql for asp .net and php , but that solution doesn't work in mysql cause in mysql we cannot limit the returned rows in a sub query !

now i asked for a solution that works in mysql too !
Reply With Quote
  #4 (permalink)  
Old 11-18-08, 11:52
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Oh, it must have been this that threw me off
Quote:
Originally Posted by georgev
What version of SQL Server?
Quote:
Originally Posted by dicanio
georgev , it is ms sql 2000.
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 11-18-08, 15:34
dicanio dicanio is offline
Registered User
 
Join Date: Nov 2008
Posts: 17
yes georgev, it was MSSQL server thread and here is my sql thread.
why dont you stop referencing threads and let me know what do you think about the way i can combine these queries in mysql ?

Reply With Quote
  #6 (permalink)  
Old 11-19-08, 15:29
dicanio dicanio is offline
Registered User
 
Join Date: Nov 2008
Posts: 17
i don't think there is no solution for my problem , but i don't know why experts doesn't discuss about it.
Reply With Quote
  #7 (permalink)  
Old 11-19-08, 15:47
dicanio dicanio is offline
Registered User
 
Join Date: Nov 2008
Posts: 17
it seems i couldn't make you understand what i am really looking for , then
let me ask you something like this

Table1 :_articles (a_id,a_cat,a_title,a_text,a_date)
Table2 :_cats(c_id,c_name)

Some Data For Table 1 :
=======================================
1 | 1 | Article 1 | Some Text1 | 01 Jun 2008
2 | 3 | Article 2 | Some Text2 | 01 Jun 2008
3 | 3 | Article 3 | Some Text3 | 01 Jun 2008
4 | 1 | Article 4 | Some Text4 | 03 Jun 2008
5 | 3 | Article 5 | Some Text5 | 03 Jun 2008
6 | 2 | Article 6 | Some Text6 | 04 Jun 2008
7 | 2 | Article 7 | Some Text7 | 05 Jun 2008
8 | 1 | Article 8 | Some Text8 | 06 Jun 2008

Some Data For Table 2 :
===================================
1 Sport
2 Math
3 History

now i need a query to show latest 2 articles for each category

Like :

Sport Cat
================
Article 8 | 06 Jun 2008
Article 4 | 03 Jun 2008

Math Cat
================
Article 7 | 05 Jun 2008
Article 6 | 04 Jun 2008

History Cat
================
Article 5 | 03 Jun 2008
Article 3 | 01 Jun 2008

Now it is clear .
Reply With Quote
  #8 (permalink)  
Old 11-19-08, 23:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT c.c_name
     , a.a_id
     , a.a_title
     , a.a_date
  FROM _cats AS c
INNER
  JOIN _articles AS a
    ON a.a_cat = c.c_id
   AND ( SELECT COUNT(*) 
           FROM _articles  
          WHERE a_cat = c.c_id
            AND a_date > a.a_date ) < 2
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 11-20-08, 01:47
dicanio dicanio is offline
Registered User
 
Join Date: Nov 2008
Posts: 17
yes , this query works but it is too slow for my tables about 1500 rows of articles and 12 rows of cats. (about 2 Secs And 266 Ms).
i am sure it will about 100000 in next months...

is it possible to tune it for better performance ?
Reply With Quote
  #10 (permalink)  
Old 11-20-08, 07:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
gots indexes?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 11-20-08, 07:56
dicanio dicanio is offline
Registered User
 
Join Date: Nov 2008
Posts: 17
yes , foreign keys have normal index and a primary and unique index for key fields.
maybe you suggest any index ?
Reply With Quote
  #12 (permalink)  
Old 11-20-08, 14:04
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
do any of those indexes include the date column?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #13 (permalink)  
Old 11-20-08, 14:35
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
I'm sure if we all wait long enough he'll eventually give us the actual indexes and then we can answer his question.
Reply With Quote
  #14 (permalink)  
Old 11-21-08, 06:33
dicanio dicanio is offline
Registered User
 
Join Date: Nov 2008
Posts: 17
here is the indexes :

for articles table :
a_id : a normal index and a unique index and it is the primary key field
a_title : a full-text index
a_date : a normal index
a_cat : a normal index

for cats table :
c_id : a normal index and a unique index and it is the primary key field
c_name : no index
Reply With Quote
  #15 (permalink)  
Old 11-21-08, 06:40
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Would it be possible to run the following MySQL commands to get a better picture?
Code:
show create table _articles;
show create table _cats;
Mike
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On