Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-21-2003, 01:27 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
a query type of questiton

Database Schema is something like this:
Code:
table Issues:
  IssueID
  ContactID
  Comments

table IssueProduct:
  IssueID
  ProductID

table Products:
  ProductID
  ProductName
I need to use the IssueProduct table so each issue can support multiple products.

I would like to query ONLY the last 50 Issues.

The problem here is that if an issue has multiple products, it will show up as as a dupplicate issue id on a new row with the other product id.

I could write programs to find for a repeated IssueID, however I can not figure out how to make it return 50 seperate issues if a multiple product issue is found.

For Example. If one of the 50 issues has 2 products assigned to it, .. then my query would only result in 49 issues.

this is a much lighter version of the actual query, with only the necessary fields to ask this question.
Code:
SELECT Issues.ContactID,Issues.Comments,Products.ProductName 
  WHERE Issues.IssueID='1' 
    AND Issues.IssueID=IssueProduct.IssueID 
    AND IssueProduct.ProductID=Products.ProductID
  ORDER BY IssueID DESC LIMIT 50
__________________
Mike
sde is offline   Reply With Quote
Old 08-21-2003, 02:19 PM   #2 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
Code:
select distinct top 50
  x,
  y
from 
  table
order by
 someid desc
?
__________________
direct entry file specification.
npa is offline   Reply With Quote
Old 08-21-2003, 02:42 PM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
top 50 works great =) i'm used to mysql. i'm using mssql for this project though, and i did not know that.

Distinct however doesn't work for this one.

i think i will have to query the product in while iterating through the results of what my first query generates.
__________________
Mike
sde is offline   Reply With Quote
Old 08-21-2003, 02:42 PM   #4 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
not sure if mysql can do this, but something along the lines of:

Code:
select *
from Issue, IssueProduct, Product
where IssueProduct.ProductID=Product.ProductID and
      IssueProduct.IssueID=Issues.IssueID and
      IssueID in 
      ( select IssueID
        from Issues
        order by IssueID desc
        limit 50 )
joe_bruin is offline   Reply With Quote
Old 08-21-2003, 02:47 PM   #5 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
The only way to do it is with mulitple queries. I can't figure a way to combine them in SQL, so you'll have to execute one query to get the last 50 Issues, then execute queries on each issue, or create on really big OR statement.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-21-2003, 02:48 PM   #6 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
I was under the impression mysql couldn't execute subqueries.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-21-2003, 04:55 PM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
i'm actually using c#/mssql for this project so that might work.

i actually wanted to make one of the fields return all the products seperated by commas, .. but i'm pretty sure that will require another query while iterating through the results.

thanks!
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mysql nested query help! kitrak2004 Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 4 08-18-2004 06:33 AM
SQL Query Help Please sde Program Design and Methods 6 03-07-2004 10:48 AM
foreach() while() for() or new mysql query? nemesis PHP 6 06-10-2003 06:57 PM
Changing variable type for edit box : mfc - .net sde Standard C, C++ 0 01-31-2003 09:53 AM
Word Wrap with Mysql type Text??? toe_cutter PHP 8 09-10-2002 12:25 PM


All times are GMT -8. The time now is 12:03 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting