MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 25th, 2013, 04:07 PM
butlimous butlimous is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 5 butlimous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 26 m 49 sec
Reputation Power: 0
Question Select articles and join its comments underneath it

I have a simple article and comments system with the following tables:

Articles table:

Code:
     id | writer  | text
     1  | Bob    | first article
     2  | Marley | second article


Comments table:

Code:
 
    id | article_id | comment
     1 |      1     |  i love this article
     2 |      1     |  good one
     3 |      2     |  waiting for more


I want to select each article with its comments underneath it. I use the following query:

Code:
 SELECT * FROM articles LEFT JOIN comments ON articles.id = comments.article_id 


The results I get:

Code:
articles.id   | articles.writer | articles.text  | comments.id | comments.article_id | comments.comment
     1             | Bob             | first article  |    1        |         1           | i love this article
     1             | Bob             | first article  |    2        |         1           | good one  
     2             | Marley          | second article |    3        |         2           | waiting for more   


What I want:

Code:
articles.id   | articles.writer | articles.text  | comments.id | comments.article_id | comments.comment
     1            | Bob             | first article  |    1        |         1           | i love this article
    NULL          | NULL            |    NULL        |    2        |         1           | good one  
     2            | Marley          | second article |    3        |         2           | waiting for more 


How do I select each article once and then the comments? Like in the first article, it has 2 comments, how to avoid the article text, writer, and id from being outputted 2 times for each comment?

Thanks

Reply With Quote
  #2  
Old January 25th, 2013, 04:43 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,870 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 22 m 32 sec
Reputation Power: 813
Hi,

do the formatting (in your case leaving out repeated values) in your application, not with SQL.

I think this is common misunderstanding caused by spreadsheet programs like Excel. SQL tables and result sets are supposed to hold data, not be pretty. In your case, (mis-)using NULL in the sense of "see above" doesn't even make sense, because your set isn't ordered. So there is no "above".

Use your JOIN to get the raw data. And then use your application to format the data in any way you want. By the way, rather than putting everything in a big table (with a lot of repetitions), I'd have the article data on top and a table of all comments below that. But that's up to you, of course.

Reply With Quote
  #3  
Old January 25th, 2013, 09:43 PM
butlimous butlimous is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 5 butlimous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 26 m 49 sec
Reputation Power: 0
Hey

I sort results but didn't add it to the query as it's out of scope of the question. Regarding data output I display each article then the comments below it not in a table like that...this is only for illustration. I can easily display the article only once but it doesn't make sense retrieving data from sql and then not displaying it...also this redundant data (repeated values) add to the memory and slow down loading the page...thats why i'm searching for a query to get only the data i need without repeated values.

Thanks

Reply With Quote
  #4  
Old January 25th, 2013, 11:47 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,870 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 22 m 32 sec
Reputation Power: 813
No offense, but I think you're really on the wrong track with that "optimization".

Have you actually done benchmarks? How many comments will you even have? I mean, more than 1,000 on a single page would be pretty much unusable.

Unless you fetch thousands of rows all into one big array, this is a complete waste of time, because the used memory doesn't even add up (PHP has a garbage collector to take care of that). And if you do collect all rows, that would be your problem.

I understand that it might seem strange to you that the values are repeated. But this is how joins in the relational database model work. If you don't like it, you might have to look for a different model.

Reply With Quote
  #5  
Old January 26th, 2013, 03:57 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,373 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 54 m 49 sec
Reputation Power: 4140
Quote:
Originally Posted by butlimous
...thats why i'm searching for a query to get only the data i need without repeated values.
here ya go... Minimize Bandwith in One-to-Many Joins
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #6  
Old January 26th, 2013, 11:00 AM
butlimous butlimous is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 5 butlimous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 26 m 49 sec
Reputation Power: 0
Thanks r937 very much I think that is exactly what I need

Reply With Quote
  #7  
Old January 29th, 2013, 02:00 PM
yateesh yateesh is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 yateesh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 22 sec
Reputation Power: 0
Select articles and join its comments underneath it

How did u insert comments into comments table for particular article ?

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Select articles and join its comments underneath it

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap