How to do this SQL Query elegantly?

Hey guys,

I need help formulating this SQL Query…

I have 2 Tables:




- Table 1 "tb_Users" with Attributes: 

    - idUser

    - NameUser


- Table 2 "tb_comment" with Attributes

    - idComment

    - idUser (FK)

    - commentText



I want to print the following output:


 - User 1

     - List of all Comments of User 1

 - User 2 

     - List of all Comments of User 2

 - ...

 - User n 

     - List of all Comments of User n

What is the most elegant way to do this?

THX

Andreas

Just read this: http://www.yiiframework.com/doc/guide/1.1/en/database.arr :)

Is this the right one for my case: http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-named-scopes ?

Unfortunatly I don’t get to how to apply it to my case… :frowning:

Have you read the guide or blog tutorial? If not, then please, do it now :) Actually it’s easy: generate models for both tables, setup relations and get all you need in a few lines of code:




$users = User::model()->with('comments')->findAll();