यह मानता है id
एक ऑटो-इन्क्रीमेंट कॉलम है:
SELECT MAX(id) AS id
FROM pms
WHERE id_to = 1 OR id_from = 1
GROUP BY (IF(id_to = 1, id_from, id_to))
मान लें कि आपके पास id_from
है और id_to
अनुक्रमित, यह भिन्नता संभवतः बेहतर प्रदर्शन करेगी क्योंकि MySQL नहीं जानता कि OR:
SELECT MAX(id) AS id FROM
(SELECT id, id_from AS id_with
FROM pms
WHERE id_to = 1
UNION ALL
SELECT id, id_to AS id_with
FROM pms
WHERE id_from = 1) t
GROUP BY id_with
यहां उन आईडी के लिए संदेश प्राप्त करने का तरीका बताया गया है:
SELECT * FROM pms WHERE id IN
(SELECT MAX(id) AS id FROM
(SELECT id, id_from AS id_with
FROM pms
WHERE id_to = 1
UNION ALL
SELECT id, id_to AS id_with
FROM pms
WHERE id_from = 1) t
GROUP BY id_with)