У меня 2 таблицыSQl Объединение двух таблиц заказанные обеими датами
Главная таблица
Id_post
Id_user_post
Post
Date_post
Вторичный стол
Id_mentioned
Id_user
Id_user_post
Id_post
Date_mentioned
У меня, например, 13 записей первой таблицы и 3 записи второй ,
Первые записи таблицы (13)
1 herman this is text 1 10:00:00 15/09/2016
2 jhon this is text 2 11:00:00 15/09/2016
3 carl this is text 3 12:00:00 15/09/2016
4 herman this is text 4 13:00:00 15/09/2016
5 herman this is text 5 14:00:00 15/09/2016
6 herman this is text 6 15:00:00 15/09/2016
7 jhon this is text 7 16:00:00 15/09/2016
8 herman this is text 8 17:00:00 15/09/2016
9 herman this is text 9 18:00:00 15/09/2016
10 carl this is text 10 19:00:00 15/09/2016
11 herman this is text 11 20:00:00 15/09/2016
12 carl this is text 12 21:00:00 15/09/2016
13 herman this is text 13 22:00:00 15/09/2016
Второй записи таблицы
1 herman jhon 7 11:20:00 15/09/2016
2 jhon carl 10 12:30:00 15/09/2016
3 herman carl 3 14:50:00 15/09/2016
Если я выбираю пост Hermann я хочу следующие результаты, упорядоченные по дате (дата публикации и даты, указанной)
1 herman this is text 1 10:00:00 15/09/2016
7 jhon this is text 7 11:20:00 15/09/20167 (date mentioned)
4 herman this is text 4 13:00:00 15/09/2016
5 herman this is text 5 14:00:00 15/09/2016
3 carl this is text 3 14:50:00 15/09/2016 (date mentioned)
6 herman this is text 6 15:00:00 15/09/2016
8 herman this is text 8 17:00:00 15/09/2016
9 herman this is text 9 18:00:00 15/09/2016
11 herman this is text 11 20:00:00 15/09/2016
13 herman this is text 13 22:00:00 15/09/2016
В этих результатах появляется сообщение, отправленное герменем и упомянутым во втором тайме BLE, по заказу date_mentioned (его somenthing как твиттер, где в профиле, в основных результатах появляется собственные сообщения и retwitts то не принадлежит одному владельцу)
Я попытался SQL JOIN остался
Select * from $table_posts left join $table_mentions on $table_posts.id_user=$table_mentions.id_user order by date_post,date_mentioned
Я тоже пробовал, но ничего ...
SELECT * FROM $table_posts WHERE id_user_post=(SELECT id_user_post FROM $table_mentions WHERE id_user_post='$id_user') AND id_user_post='$id_user' ORDER BY date_post,date_mentioned DESC
Вы используете тип данных типа datetime, верно? См. Http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Strawberry
CURRENT_TIMESTAMP() – masterhoo
Как вторая таблица относится к первому? – apokryfos