我有三张桌子,如下所示。表1包含主要收集列表(列表标题和主要注释),表2包含表1的列表和列表创建者的注释,表3包含与其他用户在表2中列表号相关的任何注释。
我已添加到SQLFiddle Here
所以输出如下:
1988年的十大专辑“只有最好的住在这里!”用户列表23
1 Who - Who the best!
Who is the best, I agree 100%! comment by User 15
I am also OK with this as #1 comment by User 47
2 Beatles - Beatles the best!
Yes I agree with your choice for number 2 spot comment by User 15
3 Rolling Stones - Rolling Stones the best!
Not too sure about this on? comment by User 15
Not should not be on this list at all! comment by User 47
等等。我希望你看到我的逻辑。但我有问题,通过PHP将这个列出如上面的例子到html表。我尝试了很多MySQL查询和子查询,但我的结果与示例无关。我不擅长复杂的查询。在桌子的最下面是我的最后一次尝试,一团糟。
表
#collection_lists
id, category_id, collectionname, collections_list_comment, username_id, created_date, approved
'75', '3', 'Top Ten Albums for 1988', 'Only the best live here!', '23', '2015-09-26 04:27:01', 'No'
#collections_items
id, collection_list_id, collection_item_number, username_id, collection_item_title, collection_item_text, created_date
'77', '75', '1', '23', 'Who', 'Who the best', '2015-09-26 04:27:01'
'78', '75', '2', '23', 'Beatles', 'Beatles the best', '2015-09-26 04:27:01'
'79', '75', '3', '23', 'Rolling Stones', 'Rolling Stones the best', '2015-09-26 04:27:01'
'80', '75', '4', '23', 'Greatful Dead', 'Greatful dead the best', '2015-09-26 04:27:01'
'81', '75', '5', '23', 'Elton John', 'EJ the best', '2015-09-26 04:27:01'
'82', '75', '6', '23', 'Tommy', 'Tommy the best', '2015-09-26 04:27:01'
'83', '75', '7', '23', 'BTO', 'BTO the best', '2015-09-26 04:27:01'
'84', '75', '8', '23', 'Eagles', 'Eagles the best', '2015-09-26 04:27:01'
'85', '75', '9', '23', 'Tommy James', 'Tommy James the best', '2015-09-26 04:27:01'
'86', '75', '10', '23', 'Bangles', 'Bangles the best', '2015-09-26 04:27:01'
#collection_item_comments
id, collections_list_id, collection_items_id, comment, username_id, commentdate
'1', '75', '1', 'Who is the best, I agree 100%!', '15', '2015-09-29'
'2', '75', '2', 'Yes I agree with your choice for number 2 spot', '15', '2015-09-29'
'3', '75', '3', 'Not too sure about this on?', '15', '2015-09-29'
'4', '75', '1', 'I am also OK with this as #1', '47', '2015-09-29'
'5', '75', '3', 'Not should not be on this list at all!', '47', '2015-09-29'
上次尝试:
select distinct collectionname, collections_list.collections_list_comment, sitelok.username, collections_list.id, collection_items.collection_item_number, collection_item_comments.comment, collection_item_comments.username_id
FROM WTTL_Main_DB.collections_list
JOIN WTTL_Main_DB.collection_items on collection_list_id = WTTL_Main_DB.collections_list.id
JOIN WTTL_Main_DB.sitelok on sitelok.id = WTTL_Main_DB.collections_list.username_id
JOIN WTTL_Main_DB.collection_item_comments on collections_list.id = collections_list.id
WHERE collections_list.id = '75' and collection_item_comments.comment IS NOT NULL;
结果:
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '1', 'Who is the best, I agree 100%!', '15'
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '1', 'Yes I agree with your choice for number 2 spot', '15'
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '1', 'Not too sure about this on?', '15'
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '1', 'Yes this is fine', '15'
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '2', 'Who is the best, I agree 100%!', '15'
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '2', 'Yes I agree with your choice for number 2 spot', '15'
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '2', 'Not too sure about this on?', '15'
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '2', 'Yes this is fine', '15'
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '3', 'Who is the best, I agree 100%!', '15'
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '3', 'Yes I agree with your choice for number 2 spot', '15'
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '3', 'Not too sure about this on?', '15'
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '3', 'Yes this is fine', '15'
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '4', 'Who is the best, I agree 100%!', '15'
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '4', 'Yes I agree with your choice for number 2 spot', '15'
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '4', 'Not too sure about this on?', '15'
'Top Ten Albums for 1988', 'Only the best live here!', 'Deadlyinx', '75', '4', 'Yes this is fine', '15'
我现在能够将collection_lists id传递到下一页,在此示例中为75。 所以我需要遍历其他两个表。
外表collections_items和内表collection_list_comments。如果我将每个select语句放入一个数组中,那么我可以将数组循环到html表。
$ query =“SELECT * FROM collection_items WHERE collection_list_id ='75'”;
$ list_array = $ link-> query($ query);
然后
$ query1 =“SELECT * FROM collection_item_comments WHERE collection_list_id ='75'”;
$ comment_array = $ link-> query($ query1);
但不确定如何正确显示结果。
Comment from user 15
Comment from user 47
Comment form user 15
Comment form user 15
Comment from user 15
Comment from user 47