我在阅读“聊天”SQL表时几乎无需帮助。
柱:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public static GameManager instance = null;
public GameObject textscoreobject;
int score;
Text scoretext;
void Awake()
{
scoretext = textscoreobject.GetComponent<Text>();
scoretext.text = "Score: " + score.ToString ();
if (instance == null)
instance = this;
else if(instance != null)
Destroy(gameObject);
}
public void Collect(int passedvalue, GameObject passedobject)
{
Destroy (passedobject);
score = score + passedvalue;
scoretext.text = "Score: " + score.ToString ();
}
}
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour {
public int value;
public float rotatespeed;
void Update ()
{
gameObject.transform.Rotate (Vector3.up * Time.deltaTime * rotatespeed);
}
void OnTriggerEnter()
{
GameManager.instance.Collect (value, gameObject);
}
}
如何获取...(每个用户的最后一条消息?)
Chat_ID - decimal(18, 0) primary key, inflexible-yes
Sent_ID - decimal(18, 0)
Receive_ID - decimal(18, 0)
Time - datetime
Message - nvarchar(MAX)
Sent_ID| Receive_ID | Time | Message
-------+----------+------------------+-----------------
1 | 2 | 11/21/2015 10:00 | Hey! test
-------+----------+------------------+-----------------
2 | 1 | 11/21/2015 10:50 | Hi! respond
-------+----------+------------------+-----------------
1 | 2 | 11/21/2015 10:51 | respond 3
-------+----------+------------------+-----------------
2 | 1 | 11/21/2015 11:05 | respond final
-------+----------+------------------+-----------------
1 | 3 | 11/21/2015 11:51 | Message 1
-------+----------+------------------+-----------------
3 | 1 | 11/21/2015 12:05 | Message 2
-------+----------+------------------+-----------------
1 | 3 | 11/21/2015 12:16 | Message Final
-------+----------+------------------+-----------------
4 | 1 | 11/21/2015 12:25 | New message 1
-------+----------+------------------+-----------------
你注意到我需要这样的东西:MAX(时间),WHERE(Sent_ID = @ Sent_ID或Receive_ID = @ Receive_ID)在这些情况下........... Sent_ID = 1 ..... Receive_ID = 1
简化:WHERE(Sent_ID = 1或Receive_ID = 1)
谢谢....
答案 0 :(得分:1)
更新:我现在明白,您希望获得两个用户之间的最新消息,无论是发送还是接收。如果是这种情况,您可以使用ROW_NUMBER
:
WITH Cte AS(
SELECT *,
rn = ROW_NUMBER() OVER(
PARTITION BY
CASE
WHEN Sent_ID > Receive_ID THEN Receive_ID
ELSE Sent_ID
END,
CASE
WHEN Sent_ID > Receive_ID THEN Sent_ID
ELSE Receive_ID
END
ORDER BY Time DESC
)
FROM chats
WHERE
Sent_ID = 1
OR Receive_ID = 1
)
SELECT
Sent_ID, Receive_ID, Time, Message
FROM Cte
WHERE rn = 1
上述查询的作用是首先按较低的ID分区聊天消息,然后按较高的ID分区。这样,您就可以确保拥有不同的ID组合。
首先,您需要获取用户发送的最后一条消息。并且UNION
它是用户从其他每个用户收到的最后消息。
SELECT c.*
FROM chats c
INNER JOIN (
SELECT
Sent_ID, MAX(Time) AS MaxTime
FROM chats
WHERE Sent_ID = 1
GROUP BY Sent_ID
) t
ON t.Sent_ID = c.Sent_ID
AND t.MaxTime = c.Time
UNION ALL
SELECT c.*
FROM chats c
INNER JOIN (
SELECT
Sent_ID, MAX(Time) AS MaxTime
FROM chats
WHERE Receive_ID = 1
GROUP BY Sent_ID
) t
ON t.Sent_ID = c.Sent_ID
AND t.MaxTime = c.Time
答案 1 :(得分:0)
问题是行不是唯一的,因为用户ID(1和2)之间的SQL Server对话与UserID(2和1)之间的对话不同,SQL服务器将其视为不同的东西。
所以我的建议是创建另一个名为
的表create table Conversation (ConvoID int, FromID int, ToID int)
insert into COnversation (ConvoID, FromID, ToID)
values (1, 1, 2),
(1, 2, 1),
(2, 1, 3),
(2, 3, 1)
然后你就可以加入#34; Chat&#34;表,并按照ConvoID和最大日期分组...因为这只是让两个用户之间的聊天变得独一无二的方式
答案 2 :(得分:0)
试试这个
select c.* from
( select User1id,User2id,max(time) as newtime
from
( select
case when sent_id < receive_id then sent_id else receive_id
end as User1id,
case when sent_id > receive_id then sent_id else receive_id
end as User2id,
time
from chats where (Sent_ID = 1 or Receive_ID = 1)
) temp
group by User1id,User2id
)t1
inner join chats c on
t1.newtime = c.time