Good evening, there is such a request:

SELECT comments.id, comments.imageid, comments.whoComment, comments.comment, comments.date, comments.time, comments.timestamp, maincomments.id, maincomments.imageid, maincomments.whoComment, maincomments.linkToAudioComment, maincomments.date, maincomments.time, maincomments.timestamp FROM comments, maincomments WHERE comments.imageid = '{$imageid}' AND maincomments.imageid = '{$imageid}' LIMIT $start, $end 

Why and why it is not important :)

Next, I want to take whoComment and find the person who left the comment (no matter in which table, they are almost similar in structure, but need to be that way). I'm trying to do it like this:

 for ($start = 0; $start < $end; $start++) { if($resultComments = mysqli_fetch_assoc($queryToGetComments)) { //Берем запись. Узнаем, кто ее написал, ищем на этого юзера информацию. $userName = $resultComments['whoComment'];//Но из какой таблицы подставится whoComment? Нужно, чтобы просто по порядку, в зависимости от сортировки в запросе (С сортировкой тоже проблема :( ) //Далее делаю, что хочу. } } 

The question is, actually, in php code, in the comments. But again, how to sort the data by date directly in the sql query? The field "Date" is in each table. I would be very grateful for the help!

Closed due to the fact that off-topic participants Ipatiev , aleksandr barakin , user194374, Mr. Black , cheops Aug 1 '16 at 7:45 pm

  • Most likely, this question does not correspond to the subject of Stack Overflow in Russian, according to the rules described in the certificate .
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 1) Use an alias for the SELECT ..., maincomments.whoComment AS mc_whocomment, ...FROM... field SELECT ..., maincomments.whoComment AS mc_whocomment, ...FROM... 2) what sorting problems? you just don't have it - 4per
  • @ 3per "But again, how to sort the data by date right in the sql query? The" Date "field is in each table." You need to sort the records by date. Those. I can write this: not ... comments.whoComment ... maincomments.whoComment and comments.whoComment as whocomment ..... maincomments.whoComment as whocomment? - Ivan Blohin
  • You have chosen the wrong way to join tables. You have a CROSS JOIN, and you need UNION [ALL]. And with him, both of your problems will disappear - 4per
  • @ 3per oooh, it seems like a fun night. The first time I hear about this: D - Ivan Blohin
  • 3
    I vote for closing this question as not relevant to the topic, because the essence of the question is directly opposite to the title (this question has no relation to sorting) - that is, the question has no benefit to other users. And at the same time, this question is a consequence of the monstrous architecture of the database, and any literal answer will only help the author to shoot himself in the foot - Ipatiev

1 answer 1

Why and why it is not important :)

Good, but most likely you will get a lot of garbage on request. In general, read about joining JOIN and UNION.

Unless explicitly stated, the order of the returned data is in no way guaranteed, that is, you can get any sequence. For explicit sorting, use ORDER BY, it is possible to specify both the direct ASC and the reverse DESC sequence.

 SELECT ... FROM ... WHERE ... ORDER BY comments.date ASC LIMIT $start, $end 
  • No, there will not be much garbage there :) How to sort, I understand, but the question is that I roughly need to collect records in a heap, and then sort them by the date field, which is in each table :) - Ivan Blohin
  • @IvanBlohin, associations are different. There are several types of JOIN: a link to a picture with 7 types , and UNION: a link to a picture - Alex Krass