473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

table relations and join

42
Hi,
I got two questions thats been bugging me but I start with the first one as its quite much to explain.
I'm trying to create a LEFT JOIN on two tables and then output everything, I have no problems getting the information but once I want to output it I get a problem.
Lets say I select a topic and then do a LEFT JOIN on comments I now output it in php like this:
[PHP]$topics = new topicBinding();
foreach($topics->getTopics() as $topic) {
...
}[/PHP]

The sql SELECT will look something like this:
[PHP]$sql = "SELECT topics.name, comments.comment FROM topics " .
"LEFT JOIN comments ON topics.id = comments.topic_id " .
"WHERE topics.id = '1'";[/PHP]

And here is the problem: because each topic have more comments I will get a new $topic for each comment instead of each topic.. so the topic would output as many times as the the num_rows of the comment,
output example:
[HTML]ID NAME COMMENT
1 first topic comment 1
1 first topic comment 2[/HTML]

I will of course not want to output the topic over and over again and instead I want to control it like this:
[PHP]$topics = new topicBinding();
foreach($topics->getTopics() as $topic) {
...
foreach($topic['comment'] as $comment) {

}
}[/PHP]
Is this possible?
Sep 15 '08 #1
7 1728
Atli
5,058 Expert 4TB
Hi.

The other option would be to get a list of all the topics and issue a separate query for each topic, to get a list of comments for the given topic.

Like, say:
Expand|Select|Wrap|Line Numbers
  1. $topics = mysqli_query("SELECT TopicID FROM topic");
  2. while($topic = mysqli_fetch_assoc($topics)) {
  3.   echo "Topic ID: ". $topicID ."\n";
  4.  
  5.   $query = "SELECT * FROM comment WHERE topicID = {$topic['TopicID']}";
  6.   $comments = mysqli_query($query);
  7.   while($comment = mysqli_fetch_assoc($comments) {
  8.     echo " - ". $comment['CommentName'] ."\n";
  9.   }
  10. }
  11.  
Which should generate a list, somewhat like this:
Expand|Select|Wrap|Line Numbers
  1. Topic ID: 1
  2.  - Comment 1
  3.  - Comment 2
  4. TopicID: 2
  5.  - Comment 3
  6.  - Comment 4
  7.  
Sep 15 '08 #2
Gozil
42
Yeah thats the way Im working right now but I thought I could do it all in one query and still get all the comments for each topic in a sub object so I could loop them inside the topic loop.
Sep 15 '08 #3
Atli
5,058 Expert 4TB
Database queries only return tables. There is really no reasonable way to get them to return something like a multi-dimensional array (beyond the 2 dimensional table format, that is.).
Sep 15 '08 #4
Gozil
42
Database queries only return tables. There is really no reasonable way to get them to return something like a multi-dimensional array (beyond the 2 dimensional table format, that is.).
Wont multiple queries slow down the process of getting the data or is it the only way to do it?
Sep 16 '08 #5
Atli
5,058 Expert 4TB
If the only way to get everything in one query is to return every row with a bunch of duplicate fields, then no. Multiple queries would probably be faster.
Sep 16 '08 #6
Gozil
42
Okay I guess I have to stick with it then :)
Sep 16 '08 #7
I remember having a situation like this in the past. I think something like CUBE or ROLLUP will work. I would give an example but that would take some digging around. I hope it's ok that I leave this for you to do. This link may help http://www.mysqldictionary.com/keywords/with-rollup

I hope this helps.

Gord
Sep 16 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run...
3
by: dbuchanan | last post by:
Hello, (Windows forms - SQL Server) I fill my datagrid with a stored procedure that includes relationships to lookup tables so that users can see the values of the combobox selections rather...
2
by: eskil | last post by:
Hi, I want to link products and contacts, products and axctivities and activities and contacts. I am using one to many links with full referential integrity. Each table is linked to a table that...
4
by: G .Net | last post by:
Hi I have a DataSet with several DataTables. I have set up relations between these tables. I want to delete all the tables and re-fill them. However, when I try to do so, even after using...
76
MMcCarthy
by: MMcCarthy | last post by:
Normalisation is the term used to describe how you break a file down into tables to create a database. There are 3 or 4 major steps involved known as 1NF (First Normal Form), 2NF (Second Normal...
0
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005 and .net 2.0. I have used 2 DirectorySearchers to retrieve Active Directory data and put them into 2 separate dataset tables. I want to create a 3rd dataset table by using the...
2
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005 and .net 2.0 I used 2 DirectorySearchers to retrieve data from Active Directory and placed them in 2 dataset tables. I need to perform the SQL equuvakebt of join operation to...
6
by: ravichoudhari | last post by:
i came accross requirement of multiple foreign keys in a table referencing the same primary key another table. i created the table relations using the relations editor in access. even though i...
0
by: katupilar | last post by:
I am writing a tool to interface with a couple of tables in SQL Server 2000. I usually write my queries with an Inner Join to bring in fields from seperate tables and load that to a DataSet. I'm...
6
by: BD | last post by:
Hi, all. I need to enforce a one-to-many relationship on 2 tables, with a join table. Say the join table contains account information. It has cust_no and acct_no. Both cust_no and acct_no are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.