這是我目前正在使用的表。
---- ------------- ------------ ---------
| id | number_from | number_to | message |
---- ------------- ------------ ---------
| 1 | 5365323350 | 5368177083 | Hello |
| 2 | 5368177083 | 5365323350 | HRU |
| 3 | 5363916551 | 5365323350 | Hola |
| 4 | 5365323350 | 5363916551 | Howdy |
---- ------------- ------------ ---------
我想要做的是選擇 number_from = 5365323350 或 number_to = 5365323350 的所有列。但是我只想顯示一個基于相反列的最新行。有點難以解釋,所以下面是我想要的結果
---- ------------- ------------ ---------
| id | number_from | number_to | message |
---- ------------- ------------ ---------
| 2 | 5368177083 | 5365323350 | HRU |
| 4 | 5365323350 | 5363916551 | Howdy |
---- ------------- ------------ ---------
所以這里是最新的行,它們在 where 部分之后沒有重復。
我對 SQL 很陌生,我已經進行了數小時的搜索,但還沒有想出辦法來做到這一點,所以希望能得到一些幫助。
uj5u.com熱心網友回復:
注意:第一部分是一個通用的解決方案,顯示任何對話中的最新訊息。特定電話號碼的解決方案在最后。
如果是我,我想我可能會嘗試想出某種唯一識別符號來聚合這些 id,這樣我就可以找到任何分組的最后一個。看看這個小提琴:
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=11a246693a7c61455b2dd17b144eb472
在這里,我使用 CONCAT 創建了一個包含兩個電話號碼的欄位,并使用了 LEAST 和 GREATEST 來確保它們是有序的。然后我可以使用電話號碼的組合來找到具有相同組合的最大 id:
SELECT
concat(
greatest(number_from, number_to),
least(number_from, number_to)
) as chatID,
max(id) as lastID
FROM table1
GROUP BY 1;
我在這里使用 GROUP BY 1 是因為 mySQL 允許您按列號指定分組,并且我不想在子句中重復連接,因為它有點笨拙。您也可以使用子查詢來執行此操作,以便您可以對可讀值進行分組:
SELECT chatID, max(id) as lastID
FROM (
SELECT id, concat(
greatest(number_from, number_to),
least(number_from, number_to)
) as chatID
FROM table1
) z GROUP BY chatID;
現在您有了聊天中最后一個條目的 ID,您只需將其用作 IN 的引數即可回傳 table1 中與這些 ID 匹配的條目。子查詢必須稍微重寫,因為它有兩列并且 IN 需要一個運算元。這是帶有子選擇的那個,因為如果有人遇到它可能會更容易閱讀:
SELECT * FROM table1 WHERE id IN (
SELECT max(id) FROM (
SELECT id, concat(
greatest(number_from, number_to),
least(number_from, number_to)
) as chatID
FROM table1
) z GROUP BY chatID
);
但是您可以輕松地使用沒有子選擇的那個:
SELECT * FROM table1 WHERE id IN (
SELECT max(id)
FROM table1
group by concat(
greatest(number_from, number_to),
least(number_from, number_to)
)
);
您還可以使用帶有 ID 的連接,如下所示:
SELECT t1.*
FROM table1 t1
JOIN (
SELECT
concat(
greatest(number_from, number_to),
least(number_from, number_to)
) as chatID,
max(id) as lastID
FROM table1
GROUP BY 1
) ids on t1.id = ids.lastID;
或者
SELECT t1.*
FROM table1 t1
JOIN (
SELECT
concat(
greatest(number_from, number_to),
least(number_from, number_to)
) as chatID,
max(id) as lastID
FROM table1
GROUP BY 1
) ids on t1.id = ids.lastID;
因此,為特定電話號碼設定規范非常簡單。您將它們添加到獲取 max(id) 的子查詢中。我只對其中一個查詢執行此操作,因為所有版本的修改都是相同的:
SELECT * FROM table1 WHERE id IN (
SELECT max(id) FROM (
SELECT id, concat(
greatest(number_from, number_to),
least(number_from, number_to)
) as chatID
FROM table1
WHERE number_to = '5365323350'
OR number_from = '5365323350'
) z GROUP BY chatID
);
這些查詢都使用您在評論中提供的小提琴中的擴展資料集。你可以使用explain來查看執行計劃。他們都評價相同。
讓我知道這是否有幫助。
uj5u.com熱心網友回復:
如果我正確理解了您的問題,您可以嘗試這樣的事情
SELECT tb1.id, sub_tb1.number_from, sub_tb1.number_to, tb1.message
FROM table1 tb1
JOIN
(select max(number_from) as number_from, number_to
from table1
WHERE number_to = 5365323350
group by number_to
)as sub_tb1 on tb1.number_from = sub_tb1.number_from
UNION ALL
SELECT tb1.id, sub_tb1.number_from, sub_tb1.number_to, tb1.message
FROM table1 tb1
JOIN
(select number_from, min(number_to) as number_to
from table1
WHERE number_from = 5365323350
group by number_from
)as sub_tb1 on tb1.number_to = sub_tb1.number_to
db小提琴鏈接
uj5u.com熱心網友回復:
聽起來你試圖描述的就像兩個人 A 和 B 之間的聊天。A 通過 ID = 1 向 B 發起聊天,B 通過 ID = 2 回應 A。所以,在這對中,你想要任何最高 ID 用于聊天對話。不知道實際背景關系,但示例看起來像您要描述的內容。根據最新的 ID,您需要該行的值。
修訂和作業查詢
select
yt2.*
from
table1 yt2
JOIN
( select
case when yt.number_from = 5365323350
then yt.number_to
else yt.number_from end ResponseTo,
max( yt.id ) lastReplyID
from
table1 yt
where
yt.number_from = 5365323350
OR yt.number_to = 5365323350
group by
case when yt.number_from = 5365323350
then yt.number_to
else yt.number_from end ) PQ
on yt2.id = PQ.LastReplyID
在這個查詢中,內部的 PRE-QUERY(pq 別名),我通過將用戶限定為 from 或 to 人來獲取僅與相關用戶關聯的記錄。分組依據是通過案例/時間。如果 from 是有問題的人,請抓住 TO 人。如果 TO 人是用戶,則獲取回復反對的 FROM 人。對于那個特定的人,以及與相關用戶的對話,獲取該對話的最新 ID。
完成后,根據正在進行的給定聊天對話的最新 ID 加入原始表。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/469062.html
上一篇:在商店頁面WooCommerce中隱藏受保護產品的價格
下一篇:檢索上一個日期的最后一個有效值
