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 05-05-2003, 01:09 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,503
sde is on a distinguished road
sql query help please

can't seem to get this happenin.

my tables are like this:
Code:
issues - issue_id,topic
issue_product - issue_id,product_id
products - product_id, product_name
i structured it this way so i can have more than one product associated with an issue.

i need to display the issue id, the topic, and all product names associated with the issue with one query .. is it possible?

for example .. this code doesn't work, but you can see where i'm going
PHP Code:
select issues.issue_idissues.topicproducts.product_name
  FROM issues
,issue_product,products
  WHERE issues
.issue_id=issue_product.issue_id 
    
AND issue_product.product_id=product.product_id
i need it to return multiple products separated by commas though .. i think i might have to do 2 queries.
__________________
Mike
sde is offline   Reply With Quote
Old 05-05-2003, 01:56 PM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
the trick to doing what you want is not to do it in the query, but in your display code. if it's php, it's really easy.

do an "order by issues.issue_id". then, when you iterate through your results, if the issue_id is the same as the previous issue_id, just print a comma and the product name. otherwise start a whole new row, printing the issue_id and topic
joe_bruin is offline   Reply With Quote
Old 05-05-2003, 02:19 PM   #3 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
Re: sql query help please

Quote:
Originally posted by sde
PHP Code:
select issues.issue_idissues.topicproducts.product_name
  FROM issues
,issue_product,products
  WHERE issues
.issue_id=issue_product.issue_id 
    
AND issue_product.product_id=product.product_id
i need it to return multiple products separated by commas though .. i think i might have to do 2 queries.
isn't it:
Code:
select
  x
from
 tblY
inner join tblZ
  on tblZ.id = tblY.id
where
  1 = 1
..? note the "inner join" ...

like joe said, you can't really get it to put them with commas however, do it in your code, much easier.
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 05-05-2003, 02:34 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,503
sde is on a distinguished road
cool thanks a lot guys .. i had already created 2 queries out of it, but i'm going to go back now and use this info.
__________________
Mike
sde is offline   Reply With Quote
Old 05-05-2003, 03:52 PM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,503
sde is on a distinguished road
that was pretty much it .. the php parsing was the tricky part. for each time it looped, i had to make the script look ahead at the next mysql result .. however .. i had to make sure it didn't check for the next mysql row when it reached the last loop , .. otherwise it generated a mysql error. fun stuff =) thanks
__________________
Mike
sde is offline   Reply With Quote
Old 05-05-2003, 03:55 PM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,503
sde is on a distinguished road
why do i need to do an inner join? here's my actual query .. joining 6 tables =)

this will be a thread stretcher .. sorry =/
Code:
SELECT details.detail_id,issues.issue_id,details.open,contacts.f_name,contacts.l_name,issues.topic,agents.nick,products.product_name
  FROM issues,details,contacts,agents,products,issue_product
  WHERE issues.issue_id=details.issue_id AND issues.contact_id=contacts.contact_id AND details.agent_id=agents.agent_id AND issue_product.product_id=products.product_id AND issue_product.issue_id=details.issue_id ORDER BY details.detail_id DESC LIMIT 10
__________________
Mike
sde is offline   Reply With Quote
Old 05-05-2003, 04:12 PM   #7 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
Quote:
Originally posted by sde
why do i need to do an inner join? here's my actual query .. joining 6 tables =)
oh, that just must be the syntax for ms sql server ...

mysql must use a diff ver of sql syntax ...
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 05-05-2003, 04:53 PM   #8 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Quote:
Originally posted by abc123
oh, that just must be the syntax for ms sql server ...

mysql must use a diff ver of sql syntax ...
Actually, MySQL uses the same sort of syntax, you can read about it here. I've yet to actually use this for anything, but it is syntatically correct to use 'INNER JOIN'.
bdl 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Forum Belisarius Feedback 2 06-14-2004 06:16 AM
SQL Query Help Please sde Program Design and Methods 6 03-07-2004 10:48 AM
a query type of questiton sde Program Design and Methods 6 08-21-2003 04:55 PM
sql question .. joining 3 tables sde PHP 8 03-03-2003 01:12 PM


All times are GMT -8. The time now is 06:13 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