SELECT disc.*, exams.id as exam_id, exams.name as exam_name, m.latest_message_author_id as latest_author_id, m.latest_message_date as latest_message_date FROM linen_discussions disc join (select orig.discussion_id, orig.user_id as latest_message_author_id, orig.date as latest_message_date from linen_discussion_messages orig join (select discussion_id, max(date) as latest_date from linen_discussion_messages group by discussion_id) x on (x.discussion_id = orig.discussion_id and orig.date = x.latest_date)) m on (m.discussion_id = disc.id) join linen_exams exams on (exams.id = disc.exam_id) WHERE disc.active=1 AND disc.exam_id = 1 AND disc.id IN (70, 72, 75, 76, 80, 78, 79, 74, 36, 81, 131, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 94, 93, 95, 96, 82, 97, 98, 99, 100, 101, 109, 107, 108, 110, 111, 104, 105, 129, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 125, 127, 126, 132, 128, 130, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 151, 152, 153, 154, 155, 160, 161, 162, 163, 164, 166, 167, 168, 169, 170, 171, 173, 174, 175, 179, 188, 212, 213, 222, 224, 232, 233, 236, 237, 241, 244, 245, 247, 248, 249, 251, 258, 268, 280, 294, 296, 308, 310, 345, 346, 354, 369, 370, 373, 374, 375, 378, 464, 390, 401, 413, 414, 460, 434, 453, 454, 458, 461, 463, 465, 466, 467, 478, 498, 510, 518, 519, 525, 532, 533, 534, 540, 544, 547, 608, 611, 558, 562, 576, 577, 588, 596, 598, 599, 602, 624, 631, 639, 645, 678, 679, 687, 689, 690, 691, 692, 693, 694, 695, 696, 697, 699, 700, 701, 702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 715, 717, 718, 720, 726, 728, 730, 732, 735, 737, 744, 745, 746, 747, 752, 762, 763, 764, 765, 766, 770, 771, 772, 784, 806, 811, 812, 843, 852, 856, 866, 913, 914, 915, 910, 918, 919, 920, 921, 931, 934, 946, 951, 956, 959, 960, 969, 970, 971, 976, 977, 978, 979, 980, 982, 983, 986, 987, 999, 1002, 1003, 1009, 1014, 1039, 1048, 1051, 1059, 1060, 1067, 1077, 1088, 1106, 1107, 1152, 1153, 1154, 1155, 1156, 1157, 1158, 1159, 1160) ORDER BY LIMIT 0, 10